gpt4 book ai didi

python - Django迁移错误: TypeError expected string or bytes-like object

转载 作者:行者123 更新时间:2023-12-03 20:26:10 25 4
gpt4 key购买 nike

我正在尝试学习Django,并且在更改模型时发生错误。我尝试了很多类似default = datetime.datetime.now的方法,但是我不知道如何解决它。

这些是我的模特

从django.db导入模型
导入日期时间

class Candidate(models.Model):
name = models.CharField(max_length=10)
introduction = models.TextField()
area = models.CharField(max_length=15)
party_number=models.IntegerField(default=0)
def __str__(self) :
return self.name

class Poll(models.Model) :
start_date = models.DateTimeField()
end_date = models.DateTimeField()
area = models.CharField(max_length=15)

class Choice(models.Model) :
poll = models.ForeignKey(Poll)
candidate = models.ForeignKey(Candidate)
votes = models.IntegerField(default=0)

当我输入commmand: python manage.py migrate时,发生此错误
Operations to perform:
Apply all migrations: admin, ang, auth, contenttypes, sessions
Running migrations:
Applying ang.0003_poll_end_date...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
self._remake_table(model, create_fields=[field])
File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
self.effective_default(field)
File "C:\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
prepared=False)
File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1438, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1417, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1275, in get_prep_value
return self.to_python(value)
File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1378, in to_python
parsed = parse_datetime(value)
File "C:\Python\Python35-32\lib\site-packages\django\utils\dateparse.py", line 93, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object

请帮我!!

最佳答案

如果您修改了模型中的字段。之后,您运行makemigrations时,它会这样询问

^C(api_env)nyros@nyros:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option: 1

我们选择1个选项,然后它将显示为
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

我们给定timezone.now()然后完成迁移。

查看您的迁移文件,其中有
class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('providers', '0023_remove_content_provider'),
]

operations = [
migrations.AddField(
model_name='content',
name='provider',
field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]

在上面的代码中观察这一行
default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)

在那一行中,forgeinkey字段的默认值为datetime,它是正确的值吗?

不,因此您必须给该字段指定一些字符串或对象的默认值。

现在,您必须在相应的迁移文件中编辑该值,如下所示
default='some string'

然后保存并运行migration命令。

尝试让我知道,是否奏效。

关于python - Django迁移错误: TypeError expected string or bytes-like object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com