[Django]-Import data into Django model with existing data?

3👍

With some help from sebpiq, I was able to get this fixed using South, natural keys, and json dumpdata.

Basically it is just a data migration using the dumped json:

datafdir = os.path.dirname(__file__)
dataf = open(os.path.join(datafdir, '0002_mh_quote_form.data.json'), 'r')
builtformfieldsjson = simplejson.loads(dataf.read())
form = BuiltForm.objects.get(pk=1)
for field in builtformfieldsjson:
    try:
        builtfield = BuiltFormField.objects.get_by_natural_key(form, field['fields']['fieldname'])
    except:
        builtfield = BuiltFormField(fieldname=field['fields']['fieldname'], builtform=form)
    for part in field['fields']:            
        if part == 'builtform':
            continue
        setattr(builtfield, part, field['fields'][part])
    builtfield.save()  

0👍

Here are 3 ideas you can dig (sorry I don’t have time to give a better answer :-S )

  • That might be a use-case for South, who knows ?

  • Check-out the chapter on serialization, deserialization and natural keys : http://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys … I know it works for foreign keys, I haven’t tried with primary keys

  • Another solution is to do the things manually :

    • dump your data with manage.py
    • copy it on the server where you want to load it
    • open ./manage.py shell and load this data manually with the deserializers, for each object deserialized set the pk to None before saving it (so a new pk will be auto-assigned).

Hope it helps !

👤sebpiq

-2👍

If the pk key has the value null in a fixture, loaddata will create a new row in the database table (allocating a new primary key value from the primary key sequence). It can be this easy.

The only complication is if the model has foreign keys. In this case, the referenced foreign key tables would have to be configured to deserialize using natural keys, as per https://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys. In your fixture, instead of using the foreign primary key values, you would use the foreign natural keys. For example, {"widget": 42} might instead be {"widget": ["XJ234245"]}. See also the section on serialization using natural keys, which is helpful in dumping fixtures with natural keys.

👤Kevin

Leave a comment