我正在开发一个在线表单生成器工具(专门针对保险代理人)。我们希望为客户提供的一件事是默认提供常用产品(汽车、家居、生活等)的预构建表单,但仍然可以修改。
在正常情况下,我只需在开发环境中创建表单,然后创建包含这些表单的固定装置,并在所有实时站点上运行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?
发布评论
评论(3)
在 sebpiq 的帮助下,我能够使用 South、自然键 和 json 转储数据 。
基本上,它只是使用转储的 json 进行数据迁移:
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:
这里有 3 个你可以挖掘的想法(抱歉我没有时间给出更好的答案 :-S )
这可能是 South,谁知道呢?
查看有关序列化、反序列化和自然键的章节:http://docs.djangoproject.com/en/dev/topics/serialization/#deserialization-of-natural-keys ...我知道它适用于外键,我还没有尝试过使用主键
另一个解决方案是手动执行操作:
./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 :
./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 !
如果固定装置中的 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.