Django - 夹具中的外键 [英] Django - Foreign Keys in Fixtures

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

问题描述

我有一个用户对象(只是默认的Django auth)对象,并且正在尝试创建一个配置文件对象的夹具。每个配置文件与用户对象具有一对一的关系,并为该用户定义一些更多的自定义内容。

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?

基本上,我试图在夹具中使用用户名用户名,而不是用户 :pk'。

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

有没有办法这样做?

推荐答案

p>您正在寻找自然键 https:// docs .djangoproject.com / en / dev / topics / serialization /#natural-keys

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

这篇关于Django - 夹具中的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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