使用现有数据将数据导入 Django 模型?

发布于 2024-11-05 16:07:23 字数 302 浏览 3 评论 0 原文

我正在开发一个在线表单生成器工具(专门针对保险代理人)。我们希望为客户提供的一件事是默认提供常用产品(汽车、家居、生活等)的预构建表单,但仍然可以修改。

在正常情况下,我只需在开发环境中创建表单,然后创建包含这些表单的固定装置,并在所有实时站点上运行syncdb。不幸的是,这是不可能的,因为我们的一些客户已经创建了表单,这可能与我的装置中的主键冲突。我还希望导出四个不同的相互关联的表格,但它们都在我的 sqformbuilder 应用程序中。

有没有一种方法可以导出固定装置,但允许它灵活地插入到数据库的另一个正在运行的副本中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

英雄似剑 2024-11-12 16:07:23

在 sebpiq 的帮助下,我能够使用 South自然键json 转储数据

基本上,它只是使用转储的 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()  

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()  
面如桃花 2024-11-12 16:07:23

这里有 3 个你可以挖掘的想法(抱歉我没有时间给出更好的答案 :-S )

  • 这可能是 South,谁知道呢?

  • 查看有关序列化、反序列化和自然键的章节:http://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys ...我知道它适用于外键,我还没有尝试过使用主键

  • 另一个解决方案是手动执行操作:

    • 使用manage.py转储你的数据
    • 将其复制到要加载的服务器上
    • 打开./manage.py shell并使用反序列化器手动加载此数据,对于每个反序列化的对象,在保存之前将pk设置为None(因此将自动分配新的pk)。

希望有帮助!

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 !

在梵高的星空下 2024-11-12 16:07:23

如果固定装置中的 pk 键值为 null,loaddata 将在数据库表中创建一个新行(从主键序列中分配一个新的主键值)。事情可以这么简单。

唯一的复杂性是模型是否有外键。在这种情况下,引用的外键表必须配置为使用自然键反序列化,按照 https://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys。在您的装置中,您将使用外部自然键,而不是使用外部主键值。例如,{"widget": 42} 可能会改为 {"widget": ["XJ234245"]}。另请参阅使用自然键序列化,这有助于转储具有自然键的装置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文