用自然键排除Django dumpdata中的主键 [英] Excluding primary key in Django dumpdata with natural keys

查看:202
本文介绍了用自然键排除Django dumpdata中的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您启用自然键,Django的dumpdata将如何排除主键?

How do you exclude the primary key from the JSON produced by Django's dumpdata when natural keys are enabled?

我已经构建了一个我想要的记录导出,以便其他人可以将其用作模板,将其加载到具有相同模式的单独数据库中,而不会与同一模型中的其他记录冲突。

I've constructed a record that I'd like to "export" so others can use it as a template, by loading it into a separate databases with the same schema without conflicting with other records in the same model.

了解Django对自然键的支持,这似乎是NK的设计目的。我的记录有一个独特的名称字段,也用作自然键。

As I understand Django's support for natural keys, this seems like what NKs were designed to do. My record has a unique name field, which is also used as the natural key.

所以当我运行:

So when I run:

from django.core import serializers
from myapp.models import MyModel
obj = MyModel.objects.get(id=123)
serializers.serialize('json', [obj], indent=4, use_natural_keys=True)

我希望输出如下:

[
    {
        "model": "myapp.mymodel", 
        "fields": {
            "name": "foo", 
            "create_date": "2011-09-22 12:00:00", 
            "create_user": [
                "someusername"
            ]
        }
    }
]

然后我可以使用loaddata加载到另一个数据库,期望动态分配一个新的主键。注意,我的create_user字段是一个FK到Django的auth.User模型,它支持自然键,它作为自然键而不是整数主键输出。

which I could then load into another database, using loaddata, expecting it to be dynamically assigned a new primary key. Note, that my "create_user" field is a FK to Django's auth.User model, which supports natural keys, and it output as its natural key instead of the integer primary key.

然而,实际产生的是:

[
    {
        "pk": 123,
        "model": "myapp.mymodel", 
        "fields": {
            "name": "foo", 
            "create_date": "2011-09-22 12:00:00", 
            "create_user": [
                "someusername"
            ]
        }
    }
]

这将清楚地与主键123冲突并覆盖任何现有记录。

which will clearly conflict with and overwrite any existing record with primary key 123.

解决这个问题的最好方法是什么?我不想追溯地将所有自动生成的主键整数字段更改为任何等效的自然键,因为这会导致性能下降以及劳动强度。

What's the best way to fix this? I don't want to retroactively change all the auto-generated primary key integer fields to whatever the equivalent natural keys are, since that would cause a performance hit as well as be labor intensive.

编辑:这似乎是一个错误,这是 reports ... 2年前...并且已经被忽略了...

This seems to be a bug that was reported...2 years ago...and has largely been ignored...

推荐答案

json 的问题是您不能省略 pk 字段,因为在再次加载灯具数据后将需要。如果不存在,json将失败,

The problem with json is that you can't omit the pk field since it will be required upon loading of the fixture data again. If not existing, json will fail with

$ python manage.py loaddata some_data.json
[...]
File ".../django/core/serializers/python.py", line 85, in Deserializer
data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])}
KeyError: 'pk'

正如回答此问题,您可以使用 yaml xml 如果你真的想省略 pk 属性 OR 只需将主键值替换为 null

As pointed out in the answer to this question, you can use yaml or xml if you really want to omit the pk attribute OR just replace the primary key value with null.

import re
from django.core import serializers

some_objects = MyClass.objects.all()
s = serializers.serialize('json', some_objects, use_natural_keys=True)
# Replace id values with null - adjust the regex to your needs
s = re.sub('"pk": [0-9]{1,5}', '"pk": null', s)

这篇关于用自然键排除Django dumpdata中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆