Components for loading and unloading data using Django’s ORM.

See Using Fixture With Django for a complete example.

class fixture.loadable.django_loadable.DjangoEnv

A wrapper around get_models to allow lookup from DataSet’s class name

static get(name, default=None)

Fetch this name from django’s cache

Parameter:name – A name like app__ModelName such that name.split(‘__’) will give you an app label and a model name suitable for get_model
class fixture.loadable.django_loadable.DjangoFixture(**kw)

Bases: fixture.loadable.loadable.DBLoadableFixture

A fixture that knows how to load DataSet objects via Django Model classes.

Django’s transaction management is implemented as a module, which is returned by create_transaction(). This means the the commit() and rollback() remain unchanged from fixture.loadable.loadable.DBLoadableFixture.

As far as mapping DataSets to models, if you don’t supply an env kwarg you’ll get the DjangoEnv class. This simply provides a get method that proxies through to django.db.models.get_model(). Alternatively you can use an inner Meta with the following attribute:


Use this on an inner Meta class to specify the model. It must be of a form that can be passed to get_model after being split on a '.' e.g:

class Meta:
    django_model = 'auth.User'

Return a new transaction

Calls enter_transaction_management and returns the django.db.transaction module (which meets the API required by fixture)

Not sure if this is needed, leaving it in for a reminder

Attach the Django model for this DataSet.

If the DataSet’s inner Meta class contains the django_model attribute then this will be used to find the model. For example:

>>> from fixture import DataSet
>>> class BookData(DataSet):
...     class Meta:
...         django_model = "yourapp.Book"
...     class dune:
...         title = "Dune"

If the DataSet’s inner Meta class contains the django_app_label attribute then a model will be looked up within that app. For example:

>>> from fixture import DataSet
>>> class BookData(DataSet):
...     class Meta:
...         django_app_label = "app"
...     class dune:
...         title = "Dune"
class fixture.loadable.django_loadable.DjangoMedium(medium, dataset)

Bases: fixture.loadable.loadable.StorageMediumAdapter

Adapter for storing data using django models

For now we’re going to say that a Fixture can only define relations and fields defined locally to the target model. So given the example models, Where Book has a ForeignKey to Author, then you’ll have a fixture like:

class BookData(DataSet):
   class Meta:
      django_model = 'app.Book'
   class book1:
      # ...
      author = AuthorData.some_author

and not like this:

class AuthorData(DataSet):
   class Meta:
      django_model = 'app.Author'
   class author1:
      # ...
      books = [BookData.book1, BookData.book2]


This might change in future as it looks like it should be possible to be able to do it the other way round (if desirable).

save(row, column_vals)

Save this row to the DB

Validates first with _check_schema()


Check that the column_vals given match up to this model’s schema

Parameter:column_vals (tuple of field_name, field_value) – The parsed column values


see the internal function column_vals inside fixture.loadable.LoadableFixture.load_dataset() for more info


This will check the field names and related model types only - it won’t validate field types

See the example models

Previous topic


Next topic