使用可为空的外键字段加载 Django fixtures

Load Django fixtures with nullable foreign key fields

我制作了一组带有两个可为空的外键字段的固定装置,但出于某种原因,当我尝试加载数据时,我收到投诉,说这些字段必须是整数。

模型(为简洁起见删除了一些内容):

class Network(models.Model):
    network     = models.GenericIPAddressField()
    mask        = models.CharField(max_length=2)
    description = models.CharField(max_length=2000)
    designation = models.CharField(choices=DESIGNATION_CHOICES, max_length=10)
    site        = models.CharField(choices=SITE_CHOICES, max_length=5)
    category    = models.CharField(choices=CATEGORY_CHOICES, max_length=10)
    vlan        = models.CharField(max_length=5, blank=True, null=True)
    vrf         = models.ForeignKey(Vrf,on_delete=models.CASCADE, blank=True, null=True)
    parent      = models.ForeignKey('self', on_delete=models.CASCADE, related_name='supernet', blank=True, null=True)

夹具块:

<django-objects version="1.0">
    <object model="ipmanager.network" pk="459124212">
        <field name="mask" type="CharField">30</field>
        <field name="site" type="CharField">place</field>
        <field name="network" type="CharField">1.1.1.1</field>
        <field name="category" type="CharField">category</field>
        <field name="description" type="CharField">enum amun set ra</field>
        <field name="vlan" type="CharField" />
        <field name="designation" type="CharField">supernet</field>
        <field name="parent" rel="ManyToOneRel" to="networks.id"></field>
        <field name="vrf" rel="ManyToOneRel" to="vrfs.id"></field>
    </object>

我试过 Null、None、空字符串和 space,django 不接受它们中的任何一个。这是我得到的错误:

>python manage.py loaddata ipmanager/net
works_fixture.xml
Traceback (most recent call last):
  File "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\db\models\fields\__init__.py", line 955, in to_python
    return int(value)
ValueError: invalid literal for int() with base 10: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File                 "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\__init__.py", line 353, in     execute_from_command_
line
    utility.execute()
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\commands\loaddata.py", line 60, in handle
    self.loaddata(fixture_labels)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\commands\loaddata.py", line 100, in loaddata
    self.load_label(fixture_label)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\management\commands\loaddata.py", line 152, in load_label
    for obj in objects:
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\serializers\xml_serializer.py", line 177, in __next__
    return self._handle_object(node)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\serializers\xml_serializer.py", line 218, in     _handle_object

    data[field.attname] = self._handle_fk_field_node(field_node, field)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\core\serializers\xml_serializer.py", line 258, in _han    dle_fk_fie
ld_node
    return     model._meta.get_field(field.remote_field.field_name).to_python(field_
value)
  File     "C:\Users\dchadwell.CORP\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\db\models\fields\__init__.py", line 960, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: ["'' value must be an integer."]

我做错了什么?

删除你不需要的外键,而不是试图找到空值,即你的固定块应该看起来像这样:

<django-objects version="1.0">
    <object model="ipmanager.network" pk="459124212">
        <field name="mask" type="CharField">30</field>
        <field name="site" type="CharField">place</field>
        <field name="network" type="CharField">1.1.1.1</field>
        <field name="category" type="CharField">category</field>
        <field name="description" type="CharField">enum amun set ra</field>
        <field name="vlan" type="CharField" />
        <field name="designation" type="CharField">supernet</field>
    </object>

接受的答案适用于外键,但是能够导入 null 仍然很有价值(如果数据库接受该字段的 null)。

为此,json 夹具应如下所示:

{
    "model": "Foo",
    "pk": "1",
    "fields":
    {
        "attribute_1": "42",
        "nullable_int_field": null,
        ....

请注意 null 周围没有引号 - 这是 json null。在那种情况下,假设 nullable_int_field 接受空值,就不会出现错误。也适用于日期字段(在夹具中以“”作为值抛出类似的错误)。