How to Load SQL Fixture in Django for User Model

How to load sql fixture in Django for User model?

Thanks for your answers. I've found the solution that works for me, and for coincidence was one of Brian's suggestion. Here it is:

Firs I disconnected the signal that created the Super User after syncdb, for I have my super user in my auth_user fixture:

models.py:

from django.db.models import signals
from django.contrib.auth.management import create_superuser
from django.contrib.auth import models as auth_app

signals.post_syncdb.disconnect(
create_superuser,
sender=auth_app,
dispatch_uid = "django.contrib.auth.management.create_superuser")

Then I created a signal to be called after syncdb:

< myproject >/< myapp >/management/__init__.py

"""
Loads fixtures for files in sql/<modelname>.sql
"""
from django.db.models import get_models, signals
from django.conf import settings
import <myproject>.<myapp>.models as auth_app

def load_fixtures(app, **kwargs):
import MySQLdb
db=MySQLdb.connect(host=settings.DATABASE_HOST or "localhost", \
user=settings.DATABASE_USER,
passwd=settings.DATABASE_PASSWORD, port=int(settings.DATABASE_PORT or 3306))

cursor = db.cursor()

try:
print "Loading fixtures to %s from file %s." % (settings.DATABASE_NAME, \
settings.FIXTURES_FILE)
f = open(settings.FIXTURES_FILE, 'r')
cursor.execute("use %s;" % settings.DATABASE_NAME)
for line in f:
if line.startswith("INSERT"):
try:
cursor.execute(line)
except Exception, strerror:
print "Error on loading fixture:"
print "-- ", strerror
print "-- ", line

print "Fixtures loaded"

except AttributeError:
print "FIXTURES_FILE not found in settings. Please set the FIXTURES_FILE in \
your settings.py"

cursor.close()
db.commit()
db.close()

signals.post_syncdb.connect(load_fixtures, sender=auth_app, \
dispatch_uid = "<myproject>.<myapp>.management.load_fixtures")

And in my settings.py I added FIXTURES_FILE with the path to my .sql file with the sql dump.

One thing that I still haven't found is how to fire this signal only after the tables are created, and not everytime syncdb is fired. A temporary work around for this is use INSERT IGNORE INTO in my sql command.

I know this solution is far from perfect, and critics/improvements/opinions are very welcome!

Regards,

Aldo

Django - Custom Sql for the User model

From the link you provided :

The hook is simple: Django just looks for a file called sql/modelname.sql, in your app directory, where modelname is the model's name in lowercase.

So, if you had a Person model in an app called myapp, you could add arbitrary SQL to the file sql/person.sql inside your myapp directory. Here's an example of what the file might contain:

How to create a fixture file

Read “Providing initial data for models”.

  1. Load some data into a Django-managed database. Simple Python scripts work nicely, Or use the default admin interface.
  2. Use manage.py dumpdata to dump the data into a JSON fixture file. Read "django-admin.py and manage.py".

How to load .sql file with manage.py

Django managment doesn't work with SQL as per documentation on initial data

A fixture is a collection of data that Django knows how to import into
a database. The most straightforward way of creating a fixture if
you’ve already got some data is to use the manage.py dumpdata command

Fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents.

Loading data is easy: just call manage.py loaddata <fixturename>,
where is the name of the fixture file you’ve created

If you still want to load data with SQL you could make data migration

Django: Possible to load fixtures with date fields based on the current date?

Fixtures just load text data files (in JSON/XML/YAML) so, there's no real way to insert dynamically generated data by just loading a fixture. On the other hand, you can get around this using other methods.

One option is the package django-fixture-generator where you can write python/django code to create data and it will be inserted before your tests are called.

Another option is a previous SO question: How to load sql fixture in Django for User model?. This has some code on using SQL files for fixtures, where you can use a SQL expression for your date requirements (e.g. GETDATE()+1 or similar in your SQL dialect).

How can I load fixtures with circular foreign keys in django?

Thanks to this post I could find a solution. Temporarily disable foreign key checks while you load in data is the best feasible solution for this issues.
Since Django doesn't provide a way to do this, we should execute raw sql code.
So, I created a data migration with django-south and the rest is in the code below:

class Migration(DataMigration):

def forwards(self, orm):
#~ Disable foreign key checks during fixture loading
from django.db import connections, DEFAULT_DB_ALIAS
connection = connections[DEFAULT_DB_ALIAS]
if 'mysql' in connection.settings_dict['ENGINE']:
cursor = connection.cursor()
cursor.execute('SET foreign_key_checks = 0')

#~ Load fixture
from django.core.management import call_command
call_command('loaddata', 'categories_fixture.json', verbosity=0)

#~ Enable foreign key checks after fixture loading
if 'mysql' in connection.settings_dict['ENGINE']:
cursor = connection.cursor()
cursor.execute('SET foreign_key_checks = 1')
connection.close()

How to use fixtures in django?

Make sure you are using manage.py /dumpdata to export the fixture.

save the fixuture under your app fixtures directory not your project directory.

name it initial_data.json and it should work for you.

when the json file does not fit to your database or is invalid, manage.py will throw an exception. I am positive that currently you didnt put the json file in the right place.



Related Topics



Leave a reply



Submit