David Wolever
David Wolever

Reputation: 154682

Django: Create fixtures without specifying a primary key?

One of the things that bugs me about Django fixtures is that you've got to specify every model's primary key. Is there any way to create fixtures without having to specify a primary key for each row?

Upvotes: 71

Views: 22723

Answers (4)

Eduardo
Eduardo

Reputation: 22832

You should have a look at Natural Keys if you're wiling to add relation without using pk's

https://docs.djangoproject.com/en/stable/ref/django-admin/#cmdoption-dumpdata-natural-foreign

Upvotes: 13

molecular
molecular

Reputation: 558

If you check out the manage.py dumpdata command, you will see some options called --natural-foreign, --natural-primary, etc. If you look at the output you can see that the objects are dumped without using primary keys or foreign keys.

Upvotes: 0

spookylukey
spookylukey

Reputation: 6576

Use "pk: null" instead of "pk: 1" (or whatever), which will result in the PK being set to None, and when the object is saved a primary key will be assigned.

This works for YAML at least, I'm guessing you're using that if you are creating by hand.

Upvotes: 101

David Wolever
David Wolever

Reputation: 154682

A friend of mine suggested the fixture module: http://farmdev.com/projects/fixture/

Upvotes: 0

Related Questions