Import data into a Django model with existing data?

I am working on an online form creation tool (especially for insurance agents). One of the things that we would like to offer our customers is that by default, ready-made forms for ordinary products (cars, homes, life, etc.) are available, but they can still be modified.

Under normal circumstances, I simply create forms in my development environment, and then create a fixture containing these forms and run syncdb on all live sites. Unfortunately, this is not an opportunity, as some of our customers have already created forms that may conflict with primary keys on my device. There are also four different related tables that I am looking to export, but all this in my sqformbuilder application.

Is there a way to export the device, but allow its flexible installation in another current copy of the database?

+4
source share
3 answers

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

Basically this is just data transfer using damped 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() 
+2
source

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

  • This could be a precedent for South , who knows?

  • Mark the chapter on serialization, deserialization, and natural keys: http://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys ... I know this works for foreign keys, I did not try with primary keys

  • Another solution is to do things manually:

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

Hope this helps!

0
source

If the pk key is null in the device, loaddata will create a new row in the database table (highlighting the new primary key value from the sequence of primary keys). It can be easy.

The only complication is that the model has foreign keys. In this case, the tables of links to external links should be configured for deserialization using natural keys, according to https://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys . In your device, instead of using the values ​​of the foreign primary key, you will use foreign natural keys. For example, {"widget": 42} could be {"widget": ["XJ234245"]} . See also the section on serialization using natural keys , which is useful when resetting fixtures using natural keys.

-1
source

All Articles