我有2款车型和优惠。汽车桌是一个充满车辆规格的现有桌子。在django管理员中,我想在规格表中将要约映射到现有汽车。这意味着当在管理员中点击某个商品时,我希望能够看到包含所有商品的列表 - 找到正确的商品 - 并将其保存在商品上。我已经通过使用基于现有汽车对象的选项列表填充foreignkey字段来完成此操作。
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的新手,刚从管理员开始。我一直在阅读关于序列化和解构的内容,但是不知道如何在这里应用它?也许我应该采取不同的方式来实现我想要的目标?
答案 0 :(得分:2)
简而言之,您根本不需要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
。
答案 1 :(得分:2)
就我而言,我想添加一个字段,如:
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)