加载带有可空外键字段的Django fixture

rn0zuynd  于 2023-05-19  发布在  Go
关注(0)|答案(3)|浏览(158)

我制作了一组具有两个可空外键字段的fixture,但是由于某种原因,当我尝试加载数据时,我收到了字段必须是整数的抱怨。
模型(为简洁起见,删除了一些内容):

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、空字符串和空格,但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."]

我做错了什么?

m2xkgtsf

m2xkgtsf1#

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

<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>
ztmd8pv5

ztmd8pv52#

接受的答案适用于外键,但是能够导入null仍然是有价值的(如果数据库接受该字段的null)。
为此,json fixture应该看起来像这样:

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

注意null周围没有引号-这是json null。在这种情况下,假设nullable_int_field接受null,则不会出现错误。还可以处理日期字段(在fixture中以“”作为值时会抛出类似的错误)。

gt0wga4j

gt0wga4j3#

除了完全删除None值字段之外,还可以使用“”(也许这也可以“”)

<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"><None></None></field>
    <field name="designation" type="CharField">supernet</field>
    <field name="parent" rel="ManyToOneRel" to="networks.id"><None></None></field>
    <field name="vrf" rel="ManyToOneRel" to="vrfs.id"><None></None></field>
</object>

相关问题