具有自然键的Django loaddata无法查询正确的外键 [英] Django loaddata with Natural Keys not querying correct Foreign Key

查看:160
本文介绍了具有自然键的Django loaddata无法查询正确的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.我正在尝试加载一些与另一个模型具有自然键关系的数据.

Here is my problem. I am trying to load some data that has a natural key relationship to another model.

我修改了父模型以生成自然键.

I modified the parent model to generate natural keys.

我使用以下命令从旧数据库中导出了数据:

I exported the the data from the old database using the command:

manage.py dumpdata resources.image -n --indent 4 > images.json

然后我尝试使用以下命令导入新数据库:

I then tried to import into the new database using the command:

manage.py loaddata images.json

这时我得到了错误:

IntegrityError: Problem installing fixtures: The row in table 'resources_image' 
with primary key '340' has an invalid foreign key: resources_image.voyage_id contains 
a value '41890' that does not have a corresponding value in voyage_voyage.id.

它正在尝试查询voyage_voyage.id而不是get_by_natural_key函数中指定的voyage_voyage.voyage_id.我仔细检查了一下,灯具中的钥匙在voyage_id字段中.下面是我的代码和示例夹具.

It is trying to query on voyage_voyage.id instead of voyage_voyage.voyage_id as specified in the get_by_natural_key function. I double checked and the key in the fixture is in the voyage_id field. Below is my code and sample fixture.

父模型:

# for parsing natural key
class VoyageManager(models.Manager):
    def get_by_natural_key(self, voyage_id):
        return self.get(voyage_id=voyage_id)


class Voyage(models.Model):

    # for parsing natural key
    objects = VoyageManager()


    voyage_id = models.IntegerField("Voyage ID (can be empty)", null=True, blank=True)

    # A WHOLE BUNCH OF FIELDS

    # generate natural key
    def natural_key(self):
        return (self.voyage_id)


    class Meta:
        ordering = ['voyage_id',]
        verbose_name = 'Voyage'
        verbose_name_plural = "Voyages"

    def __unicode__(self):
        return "Voyage #%s" % str(self.voyage_id)

子模型:

class Image(models.Model):
    voyage = models.ForeignKey(Voyage, null=True, blank=True)

    # MANY OTHER FIELDS

    class Meta:
        verbose_name = "Image"
        verbose_name_plural = "Images"

        ordering = ["date"]

治具:

{
    "pk": 340, 
    "model": "resources.image", 
    "fields": {
        "category": 56, 
        "voyage": 41890, 
        "date": 1873, 
        "description": "blah blah blah", 
        "language": "  ", 
        "creator": null, 
        "title": "Catherine Zimmermann-Mulgrave, \nc.1873", 
        "source": "blah blah blah", 
        "ready_to_go": true, 
        "file": "images/5AF81DA065049ACE0EC8E236C445F5BC.JPG", 
        "order_num": 0
    }
}

推荐答案

所以这才是我最终想出的

So here is what I finally figured out

此函数希望输入来自元组:

This function expects the input to come from a tuple:

def get_by_natural_key(self, voyage_id):
        return self.get(voyage_id=voyage_id)

当指定-natural 标志时,此函数会生成元组

This function produces the tuple when --natural flag is specified

def natural_key(self):
        return (self.voyage_id)

这里的窍门是,当您有一个只有一个元素的元组时,它会恢复为原始类型(str,int等),以强制单个元素元组,您需要在结尾加上,",因此解决方法是:

The trick here is that when you have a tuple with only one element it reverts back to its original type (str, int etc.) to force a single element tuple you need a trailing "," so the fix is :

def natural_key(self):
        return (self.voyage_id,)

这篇关于具有自然键的Django loaddata无法查询正确的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆