Django loaddata with Natural Keys not querying correct Foreign Key

随声附和 提交于 2019-12-05 12:44:40
Alex

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)

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

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

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,)

the question is wether

{
    "pk": 41890, 
    "model": "voyage.voyage", 
    "fields": {
        ...
    }
}

is in images.json file, if not I guess you'll have to include it in the dump somehow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!