Django - 装置中的外键
我有一个“User”对象的固定装置(只是默认的 Django auth 对象),并且正在尝试创建一个“Profile”对象的固定装置。每个配置文件都与用户对象具有一对一的关系,并为该用户定义了一些更多的自定义内容。
据我所知,处理固定装置中的外键的正常方法是将外来对象的主键硬编码到固定装置中。有没有办法避免这样做?
基本上,我试图在固定装置中使用类似 '"user":username' 的内容,而不是 '"user":pk' 。
有什么办法可以做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要做的是使用自然键进行序列化,此处解释。
您基本上需要定义如何从这些特殊管理器和模型方法中的其他字段获取对象。
才发现这个问题9年前就有人问了,什么?为什么它出现在提要中的顶部?
What you are trying to do is serializing with natural keys, explained here.
You basically need to define how to get the object from other fields in these special manager and model methods.
Just realized that this question has been asked 9 years ago, what? Why did it appear in the feed at the top?