问题
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 show me how this can be done?
I want to be able to do this because at this point, it seems like the only way to have the 'exact' same data is to manually insert them into the database ourselves.
I want some 'nice' automated way of inserting rows in a table.
回答1:
You'd want to use fixtures.
- Create a fixtures directory in your app's folder.
- Create a dictionary file in that folder, intial_data.json (or XML/YAML)
- Populate the file with the data you'd want to insert. Example
- 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.
回答2:
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)
来源:https://stackoverflow.com/questions/11868102/can-the-south-for-django-insert-rows-of-data-into-database