Django - Foreign Keys in Fixtures

白昼怎懂夜的黑 提交于 2019-12-06 18:30:01

问题


I have a fixture of "User" objects (just the default Django auth ones), and am trying to create a fixture of "Profile" objects. Each profile has a one-to-one relation to a user object, and defines some more custom stuff for that user.

As far as I can tell, the normal way of dealing with foreign keys in a fixture is to just hardcode the primary key of the foreign object into the fixture. Is there a way to avoid doing that?

Basically, I'm trying to have something like '"user":username' in the fixture rather than '"user":pk'.

Is there any way to do this?


回答1:


You're looking for "Natural Keys" https://docs.djangoproject.com/en/dev/topics/serialization/#natural-keys



来源:https://stackoverflow.com/questions/7897211/django-foreign-keys-in-fixtures

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