Tim Akinbo's Tumblelog RSS

Hi, I'm Tim Akinbo and this is my tumblelog.

Afrigator

My Popularity (by popuri.us)

Archive

Jan
28th
Fri
permalink

Creating filtered Django fixtures

If you ever find yourself needing to create django fixtures from a select records in your database, here are steps that can help.

For the model you intend to export, you’ll need to modify your default object manager. I’ve include a snippet below showing an object manager with a modified get_query_set method:

 class LocationManager(models.Manager):
      def get_query_set(self):
          return super(LocationManager, self).get_query_set().filter(type__name=”Registration Center”)

Next, set your default object manager to this:

class Location(models.Model):

    objects = LocationManager()

Now you can do a ./manage.py dumpdata locations.Location and your generated fixture will be filtered based on your modified get_query_set.

Comments (View)

blog comments powered by Disqus