Import data into Django model with existing data?

风流意气都作罢 提交于 2020-01-14 13:12:17

问题


I'm working on an online form builder tool (specifically for insurance agents). One of the things we would like to offer our customers is to have pre-built forms for common products (auto, home, life, etc) be available by default, but still modifiable.

Under normal circumstances, I would simply create the forms in my development environment, then create a fixture containing these forms, and run syncdb on all the live sites. Unfortunately that isn't a possibility, as some of our customers already have created forms, which may conflict with the primary keys in my fixture. There are also four different inter-related tables that I am looking to export, but it is all in my sqformbuilder app.

Is there a way to export a fixture but allow it to be flexibly inserted into another running copy of the database?


回答1:


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()  



回答2:


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 !




回答3:


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.



来源:https://stackoverflow.com/questions/5940294/import-data-into-django-model-with-existing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!