Django - Foreign Keys in Fixtures
Asked Answered
S

1

29

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?

Shortly answered 25/10, 2011 at 23:51 Comment(1)
When you say "fixture" are you referring to the json/yaml format fixtures that the django TestCase is looking for and that can also be loaded with django manage loaddata?Gintz
K
12

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?

Krever answered 20/7, 2020 at 21:10 Comment(1)
Hey it helped me so thanks for replying even after 9 years! =)Milicent

© 2022 - 2024 — McMap. All rights reserved.