model.save() not called when loading Django fixtures?

1.6k Views Asked by At

I am overriding my Django model save() method, so I can do some extra sanity checking on the object. (Is save() the correct place to do this?)

It doesn't appear that my fixtures/initial_fixtures.yaml objects have their save() method called. How can I sanity-check my fixtures?

3

There are 3 best solutions below

6
On

Your fixtures are assumed to be good data, not questionable input, so I'm not sure of a good case when you would need to be sanity checking them.

You can add data to your db through the admin or something within your app and then export that as a fixture, if you need to do some one-time initial validation.

0
On

The .save() method is called during fixture loading as seen in https://code.djangoproject.com/browser/django/tags/releases/1.3.1/django/core/management/commands/loaddata.py?rev=17029#L174

If you use a different DJ version, you can check that but I'm quite sure it is called in older versions as well.

How are you checking if your objects have their save() method called?

And about doing this in .save(), if the sanity checks are non-trivial then I don't think it's a very good idea.

1
On

As of Django 1.5, save() is NOT called:

When fixture files are processed, the data is saved to the database as is. Model defined save() methods are not called, and any pre_save or post_save signals will be called with raw=True since the instance only contains attributes that are local to the model.

https://docs.djangoproject.com/en/1.9/ref/django-admin/