开发者

Import data into Django model with existing data?

开发者 https://www.devze.com 2023-03-04 20:41 出处:网络
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,

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, w开发者_JS百科hich 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?


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


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 !


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消