用自然键排除Django dumpdata中的主键

编程入门 行业动态 更新时间:2024-10-10 17:23:22
本文介绍了用自然键排除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中的主键

本文发布于:2023-10-27 23:58:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   自然   Django   dumpdata

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!