我在models.py上编写了这段代码,但是当我尝试从Question中保存对象时,在外壳程序中出现了此错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\base.py", line 718, in save
force_update=force_update, update_fields=update_fields)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\base.py", line 748, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\base.py", line 831, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\base.py", line 869, in _do_insert
using=using, raw=raw)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\query.py", line 1136, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\sql\compiler.py", line 1288, in execute_sql
for sql, params in self.as_sql():
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\sql\compiler.py", line 1241, in as_sql
for obj in self.query.objs
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\sql\compiler.py", line 1241, in <listcomp>
for obj in self.query.objs
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\sql\compiler.py", line 1240, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\sql\compiler.py", line 1182, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\fields\__init__.py", line 1429, in get_db_prep_value
value = self.get_prep_value(value)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\fields\__init__.py", line 1408, in get_prep_value
value = super().get_prep_value(value)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\fields\__init__.py", line 1268, in get_prep_value
return self.to_python(value)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\db\models\fields\__init__.py", line 1369, in to_python
parsed = parse_datetime(value)
File "H:\virtualEnvs\Django_1\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
这是我的models.py代码:
from django.db import models
from django.utils import timezone
class Question(models.Model):
text=models.CharField(max_length=300)
pub_date=models.DateTimeField(default=timezone.now)
def __str__(self):
return self.text
class Choice(models.Model):
question=models.ForeignKey(Question,on_delete=models.CASCADE)
text=models.CharField(max_length=300)
vote=models.IntegerField(default=0)
def __str__(self):
return self.text