创建测试数据库时,Django测试失败

时间:2012-04-07 20:50:51

标签: python django postgresql django-testing

它失败了许多与related_name相关的问题。当然,当运行实际的开发服务器(使用./manage.py runserver)时,这不会发生,我猜测模型可能会生成两次?我正在运行psycopg2 2.4.1,这是我的测试输出:

(venv)lacrymology@Roller:boilerplate$ ./manage.py test
Creating test database for alias 'default'...
Error: One or more models did not validate:
l10n.address: Accessor for field 'user_shipping' clashes with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
l10n.address: Reverse query name for field 'user_shipping' clashes with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
l10n.address: Accessor for field 'user_billing' clashes with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
l10n.address: Reverse query name for field 'user_billing' clashes with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
custom_registration.profile: Accessor for field 'user' clashes with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
custom_registration.profile: Reverse query name for field 'user' clashes with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
addressmodel.address: Accessor for field 'user_shipping' clashes with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
addressmodel.address: Reverse query name for field 'user_shipping' clashes with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
addressmodel.address: Accessor for field 'user_billing' clashes with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
addressmodel.address: Reverse query name for field 'user_billing' clashes with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
profiles.profile: Accessor for field 'user' clashes with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
profiles.profile: Reverse query name for field 'user' clashes with related field 'User.profile'. Add a related_name argument to the definition for 'user'.

2 个答案:

答案 0 :(得分:1)

您的test.py极有可能导入一个不在INSTALLED_APP内但有一些模型字段指向碰撞模型的应用程序。尝试在test.py中导入但不在INSTALLED_APPS内的应用程序中grep user_shipping ,并替换它。

./manage.py validate,将由./manage.py runserver在内部调用,仅检查从INSTALLED_APP内的应用加载的模型,因此在运行devserver时没有错误。但是,test.py中的导入会导致额外的非安装应用程序模型(如果有的话)被加载,然后发生冲突。

答案 1 :(得分:0)

这是django-shop测试的问题,如本评论中所述:Django test fails when creating test database

在没有更改应用程序代码的情况下,我无法找到它,所以我最终只测试了自己的应用程序(运行./manage.py test myapp而不是./manage.py test