Skip to content Skip to sidebar Skip to footer

Can The South (for Django) Insert Rows Of Data Into Database?

I want to create a table, and then a list of data that should be inserted into that table. Does South have the capability to do such a thing? If so, do you have any reference as to

Solution 1:

You can! It's called a "data migration".

There are plenty of times you might want to use one: the link above gives a good example, another is the "data migration for every Django project":

from south.v2 import DataMigration
from django.conf import settings

class Migration(DataMigration):

    def forwards(self, orm):
        Site = orm['sites.Site']
        site = Site.objects.get(id=settings.SITE_ID)
        site.domain = settings.DOMAIN_NAME
        site.name = settings.SITE_NAME
        site.save()

(this picks up the domain and site name from settings.py, for use with the sites framework)


Solution 2:

You'd want to use fixtures.

  1. Create a fixtures directory in your app's folder.
  2. Create a dictionary file in that folder, intial_data.json (or XML/YAML)
  3. Populate the file with the data you'd want to insert. Example
  4. Run manage.py loaddata <fixturename>, where <fixturename> is the name of the fixture file you've created.

South handles this pretty much the same way, but it seems like Django's core approach is more documented.


Post a Comment for "Can The South (for Django) Insert Rows Of Data Into Database?"