在创建模型

时间:2018-06-11 00:52:33

标签: django string django-models

我有一个模型,我正在尝试创建一个新的实例。但是,当我运行基本的create命令时,我收到一个错误。 C.objects.create()。我仍然可以通过管理员创建一个实例,但似乎不能在它之外。

代码运行:

qs_a = A.objects.all().first()
qs_b = B.objects.all().first()
C.objects.create(a=qs_a, b=qs_b)

模型

class C(models.Model):
    a                    = models.ForeignKey(A, blank=True, default=False)
    b                    = models.ForeignKey(B, blank=True, default=False)
    the_id               = models.CharField(max_length=120, blank=True) # AB31DE3
    status               = models.CharField(max_length=120, default='initiated', choices=STATUS_CHOICES)
    start_date           = models.DateField(default=False, blank=True, null=True)
    end_date             = models.DateField(default=False, blank=True, null=True)
    ...

    updated              = models.DateTimeField(auto_now=True)
    timestamp            = models.DateTimeField(auto_now_add=True)

堆栈跟踪:

>> C.objects.create(a=qs_a, b=qs_b)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\myApp\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\myApp\lib\site-packages\django\db\models\query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\myApp\lib\site-packages\django\db\models\base.py", line 807, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\myApp\lib\site-packages\django\db\models\base.py", line 837, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\myApp\lib\site-packages\django\db\models\base.py", line 923, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\myApp\lib\site-packages\django\db\models\base.py", line 962, in _do_insert
    using=using, raw=raw)
  File "C:\myApp\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\myApp\lib\site-packages\django\db\models\query.py", line 1076, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\myApp\lib\site-packages\django\db\models\sql\compiler.py", line 1106, in execute_sql
    for sql, params in self.as_sql():
  File "C:\myApp\lib\site-packages\django\db\models\sql\compiler.py", line 1059, in as_sql
    for obj in self.query.objs
  File "C:\myApp\lib\site-packages\django\db\models\sql\compiler.py", line 1059, in <listcomp>
    for obj in self.query.objs
  File "C:\myApp\lib\site-packages\django\db\models\sql\compiler.py", line 1058, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\myApp\lib\site-packages\django\db\models\sql\compiler.py", line 998, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\myApp\lib\site-packages\django\db\models\fields\__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "C:\myApp\lib\site-packages\django\db\models\fields\__init__.py", line 1301, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\myApp\lib\site-packages\django\db\models\fields\__init__.py", line 1296, in get_prep_value
    return self.to_python(value)
  File "C:\myApp\lib\site-packages\django\db\models\fields\__init__.py", line 1258, in to_python
    parsed = parse_date(value)
  File "C:\myApp\lib\site-packages\django\utils\dateparse.py", line 61, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

你看到这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

错误说:

File "C:\myApp\lib\site-packages\django\utils\dateparse.py", line 61, in parse_date

在您的模型中,日期有问题:

start_date           = models.DateField(default=False, blank=True, null=True)
end_date             = models.DateField(default=False, blank=True, null=True)

DateField的默认值是日期或字符串而不是False(bool类型),因此将其更改为:

from django.utils import timezone

start_date           = models.DateField(default=timezone.now, blank=True, null=True)
end_date             = models.DateField(default=timezone.now, blank=True, null=True)

或禁用默认值:

start_date           = models.DateField(blank=True, null=True)
end_date             = models.DateField(blank=True, null=True)

然后makemigrations并迁移