似乎无法在任何地方找到我的问题的答案。你是我唯一的希望。所以我有一个已经迁移的现有模型。我想为此模型添加一个外键,将其链接到'auth.User'表。这就是我的代码:
from django.db import models
from django.contrib.auth.models import User
class Beer(models.Model):
created = models.DateTimeField(auto_now_add=True)
brand = models.CharField(max_length = 100,default='')
beer_type = models.CharField(max_length = 100,default='')
ml = models.IntegerField(default=330)
owner = models.OneToOneField(User,default=User.objects.get(pk=2), related_name="beers")
然后我尝试创建迁移文件:
python manage.py makemigrations
我收到以下错误:
Migrations for 'beers':
0003_beer_owner.py:
- Add field owner to beer
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management /__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 87, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
ValueError: Cannot serialize: <User: user>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations /#migration-serializing
有任何线索如何修复它?它可能与我使用的默认值有关。显然,用户对象无法序列化。 感谢
答案 0 :(得分:2)
您需要提供数据库等效值,并且由于您将其链接到主键,因此您需要提供主键作为默认值:
owner = models.OneToOneField(User,
default=User.objects.get(pk=2).pk,
related_name="beers")
或者,您可以通过直接提供整数值来简化整个事物并保留它:
owner = models.OneToOneField(User, default=2, related_name="beers")
不言而喻;你应该确保你在数据库中拥有具有该id的用户;理想情况下使用数据迁移。