繁体   English   中英

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

[英]Django test fails when creating test database

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

(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'.

很有可能您的 test.py 导入的应用程序不在 INSTALLED_APP 内,但有一些 model 字段指向冲突的 model。尝试在 test.py 中导入但不在 INSTALLED_APPS 内的应用程序内使用 grep user_shipping ,然后替换它。

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

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

如果不更改应用程序的代码,我找不到解决它的办法,所以我最终只测试了我自己的应用程序(运行./manage.py test myapp而不是./manage.py test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM