Micah Pearce
Micah Pearce

Reputation: 1935

Django expected string or bytes-like object when creating model

I have a model that I'm trying to create a new instance of. However, I'm getting an error when I'm running the basic create command. C.objects.create(). I can still create a instance through the admin, but can't seem to outside of it.

Code Run:

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

model

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)

stacktrace:

>> 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

Do you see what is going on here?

Upvotes: 0

Views: 3538

Answers (1)

Ykh
Ykh

Reputation: 7717

the error says:

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

there is something wrong with date, in your model:

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

the DateField's default is date or string not False(bool type),so change it to :

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)

or disable default like:

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

then makemigrations and migrate

Upvotes: 2

Related Questions