Django 模型 - 无法序列化为迁移文件
Django Model - cannot serialize into migration files
我有 2 款车型 Car 和 Offer。汽车 table 是现有的 table,其中填满了车辆规格。在 django 管理员中,我想将报价映射到规格 table 中的现有汽车。这意味着当在管理员中点击报价时,我希望能够看到包含所有汽车的列表 - 找到正确的 - 并将其保存在报价中。我通过使用基于现有汽车对象的选择列表填充外键字段来完成此操作。
models.py:
class Car(models.Model):
brand = models.TextField(max_length=300, default= "")
model = models.TextField(max_length=300, default= "")
edition = models.TextField(max_length=300, default= "")
engineVolume = models.FloatField(default=0.0)
def __unicode__(self):
return smart_unicode(self.brand)
carIds = []
idx = 1
for car in Car.objects.all():
carIds.append((idx, car))
idx = idx + 1
class Offer(models.Model):
stringUrl = models.TextField(max_length=300)
extractionDate = models.DateTimeField(default=datetime.datetime.now, blank=True)
cars = models.ForeignKey(Car, default= "", choices=carIds, null=True, to_field='id')
这非常有效。我在管理员中单击一个报价,然后看到一个选择框,其中填充了所有现有汽车。我找到了正确的汽车,将其保存,数据库中优惠的外键汽车 ID 指向正确的车辆。
但是当我想稍后进行迁移时,django 突然说它无法序列化汽车对象?
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site- packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-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/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 87, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
`enter code here`ValueError: Cannot serialize: <Car: Nissan>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration- serializing
我不知道为什么会出现这个错误。我是 django 的新手,刚开始使用管理员。我一直在阅读有关序列化和解构的文章,但看不到如何在此处应用它?也许我应该走不同的路线来实现我想要的?
简而言之,您根本不需要 choices
。
choices
,虽然可以是任何可迭代的并且可以修改,但更适合静态数据。
之后,这段代码
carIds = []
idx = 1
for car in Car.objects.all():
carIds.append((idx, car))
idx = idx + 1
一事无成。
首先,您没有以任何方式过滤选项,只是将它们转换为元组列表。其次,ForeignKey
会自动从引用模型的未过滤查询集中提供选择,例如Car.objects.all()
.
所以您可以删除 choices
,如果您需要在 ModelForm
和管理员中进行过滤,请改用 ForeignKey.limit_choices_to
。
在我的例子中,我尝试添加一个字段,例如:
task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first())
但使用以下方法更正了此问题:
task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first().pk)
我有 2 款车型 Car 和 Offer。汽车 table 是现有的 table,其中填满了车辆规格。在 django 管理员中,我想将报价映射到规格 table 中的现有汽车。这意味着当在管理员中点击报价时,我希望能够看到包含所有汽车的列表 - 找到正确的 - 并将其保存在报价中。我通过使用基于现有汽车对象的选择列表填充外键字段来完成此操作。
models.py:
class Car(models.Model):
brand = models.TextField(max_length=300, default= "")
model = models.TextField(max_length=300, default= "")
edition = models.TextField(max_length=300, default= "")
engineVolume = models.FloatField(default=0.0)
def __unicode__(self):
return smart_unicode(self.brand)
carIds = []
idx = 1
for car in Car.objects.all():
carIds.append((idx, car))
idx = idx + 1
class Offer(models.Model):
stringUrl = models.TextField(max_length=300)
extractionDate = models.DateTimeField(default=datetime.datetime.now, blank=True)
cars = models.ForeignKey(Car, default= "", choices=carIds, null=True, to_field='id')
这非常有效。我在管理员中单击一个报价,然后看到一个选择框,其中填充了所有现有汽车。我找到了正确的汽车,将其保存,数据库中优惠的外键汽车 ID 指向正确的车辆。 但是当我想稍后进行迁移时,django 突然说它无法序列化汽车对象?
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site- packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-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/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 143, in handle
self.write_migration_files(changes)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 171, in write_migration_files
migration_string = writer.as_string()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 166, in as_string
operation_string, operation_imports = OperationWriter(operation).serialize()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 124, in serialize
_write(arg_name, arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 87, in _write
arg_string, arg_imports = MigrationWriter.serialize(_arg_value)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 377, in serialize
return cls.serialize_deconstructed(path, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 268, in serialize_deconstructed
arg_string, arg_imports = cls.serialize(arg)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 303, in serialize
item_string, item_imports = cls.serialize(item)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/writer.py", line 465, in serialize
"topics/migrations/#migration-serializing" % (value, get_docs_version())
`enter code here`ValueError: Cannot serialize: <Car: Nissan>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/1.8/topics/migrations/#migration- serializing
我不知道为什么会出现这个错误。我是 django 的新手,刚开始使用管理员。我一直在阅读有关序列化和解构的文章,但看不到如何在此处应用它?也许我应该走不同的路线来实现我想要的?
简而言之,您根本不需要 choices
。
choices
,虽然可以是任何可迭代的并且可以修改,但更适合静态数据。
之后,这段代码
carIds = []
idx = 1
for car in Car.objects.all():
carIds.append((idx, car))
idx = idx + 1
一事无成。
首先,您没有以任何方式过滤选项,只是将它们转换为元组列表。其次,ForeignKey
会自动从引用模型的未过滤查询集中提供选择,例如Car.objects.all()
.
所以您可以删除 choices
,如果您需要在 ModelForm
和管理员中进行过滤,请改用 ForeignKey.limit_choices_to
。
在我的例子中,我尝试添加一个字段,例如:
task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first())
但使用以下方法更正了此问题:
task = models.ForeignKey(Task, on_delete=models.CASCADE, default=Task.objects.first().pk)