在Django中加载fixture时,内容类型的问题 [英] Problems with contenttypes when loading a fixture in Django

查看:177
本文介绍了在Django中加载fixture时,内容类型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于内容冲突,我无法将Django fixture加载到MySQL数据库中。首先,我尝试从我的应用程序转储数据,如下所示:

  ./ manage.py dumpdata escola> fixture.json 

但是我一直在缺少外键问题,因为我的应用程序escola使用表其他应用。我不断添加其他应用程序,直到我得到这个:

  ./ manage.py dumpdata contenttypes auth escola> fixture.json 

当我尝试加载数据作为测试时,现在的问题是以下约束违规固定装置:

  IntegrityError:(1062,键2的重复输入escola-t23aluno)

似乎问题是Django正试图通过不同的主键值动态重新创建内容类型,这些值与主键值冲突夹具。这似乎与此处记录的错误相同: http://code.djangoproject.com/ticket/7052



问题是,推荐的解决方法是转储我已经在做的contenttypes应用程序!是什么赋予了?如果有任何区别,我确实有一些自定义模型权限,如下所述: http://docs.djangoproject.com/en/dev/ref/models/options/#permissions

解决方案

manage.py dumpdata --natural 将使用更耐用的外键表示。在django他们被称为自然键。例如:




  • Permission.codename 用于 Permission.id

  • User.username 用于 User.id



阅读更多:序列化django对象中的自然键部分



其他一些 c code code code code code code code code code code $ c $ 4 使其可读取。

  • -e sessions 排除会话数据

  • -e admin 排除管理员站点上的管理操作历史

  • -e contenttypes -e auth.Permission 排除在 syncdb 期间每次自动从架构重新创建的对象。只能与 - 自然一起使用,否则您可能会遇到不正确的ID号码。


  • I am having trouble loading Django fixtures into my MySQL database because of contenttypes conflicts. First I tried dumping the data from only my app like this:

    ./manage.py dumpdata escola > fixture.json
    

    but I kept getting missing foreign key problems, because my app "escola" uses tables from other applications. I kept adding additional apps until I got to this:

    ./manage.py dumpdata contenttypes auth escola > fixture.json
    

    Now the problem is the following constraint violation when I try to load the data as a test fixture:

    IntegrityError: (1062, "Duplicate entry 'escola-t23aluno' for key 2")
    

    It seems the problem is that Django is trying to dynamically recreate contenttypes with different primary key values that conflict with the primary key values from the fixture. This appears to be the same as bug documented here: http://code.djangoproject.com/ticket/7052

    The problem is that the recommended workaround is to dump the contenttypes app which I'm already doing!? What gives? If it makes any difference I do have some custom model permissions as documented here: http://docs.djangoproject.com/en/dev/ref/models/options/#permissions

    解决方案

    manage.py dumpdata --natural will use a more durable representation of foreign keys. In django they are called "natural keys". For example:

    • Permission.codename is used in favour of Permission.id
    • User.username is used in favour of User.id

    Read more: natural keys section in "serializing django objects"

    Some other useful arguments for dumpdata:

    • --indent=4 make it human readable.
    • -e sessions exclude session data
    • -e admin exclude history of admin actions on admin site
    • -e contenttypes -e auth.Permission exclude objects which are recreated automatically from schema every time during syncdb. Only use it together with --natural or else you might end up with badly aligned id numbers.

    这篇关于在Django中加载fixture时,内容类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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