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

时间:2022-04-22 01:23:34

It fails with a number of related_name-related issues. Of course when running the actual dev server (with ./manage.py runserver) this doesn't happen, I'm guessing maybe the models are being generated twice? I'm running psycopg2 2.4.1, and here's my test output:

它失败了许多与related_name相关的问题。当然,当运行实际的开发服务器(使用./manage.py运行服务器)时,这不会发生,我猜可能模型正在生成两次?我正在运行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' *es 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' *es with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
l10n.address: Accessor for field 'user_billing' *es 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' *es with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
custom_registration.profile: Accessor for field 'user' *es with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
custom_registration.profile: Reverse query name for field 'user' *es with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
addressmodel.address: Accessor for field 'user_shipping' *es 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' *es with related field 'User.shipping_address'. Add a related_name argument to the definition for 'user_shipping'.
addressmodel.address: Accessor for field 'user_billing' *es 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' *es with related field 'User.billing_address'. Add a related_name argument to the definition for 'user_billing'.
profiles.profile: Accessor for field 'user' *es with related field 'User.profile'. Add a related_name argument to the definition for 'user'.
profiles.profile: Reverse query name for field 'user' *es with related field 'User.profile'. Add a related_name argument to the definition for 'user'.

2 个解决方案

#1


1  

It is highly possible that your test.py imports an app which is not inside INSTALLED_APP but has some model fields pointing to the collided model. Try to grep user_shipping inside apps which are imported in test.py but not inside INSTALLED_APPS, and replace it.

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

./manage.py validate, which will be called internally by ./manage.py runserver, only checks models loaded from apps inside INSTALLED_APP and thus there was no error when you ran devserver. However, imports inside test.py will cause extra models of non-installed apps, if there is any, to be loaded and then collision will happen.

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

#2


0  

This was an issue with django-shop tests, as described in this comment: Django test fails when creating test database

这是django-shop测试的问题,如本评论中所述:Django测试在创建测试数据库时失败

I could find no way around it without changing the app's code, so I ended up just testing my own apps (running ./manage.py test myapp instead of ./manage.py test

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

#1


1  

It is highly possible that your test.py imports an app which is not inside INSTALLED_APP but has some model fields pointing to the collided model. Try to grep user_shipping inside apps which are imported in test.py but not inside INSTALLED_APPS, and replace it.

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

./manage.py validate, which will be called internally by ./manage.py runserver, only checks models loaded from apps inside INSTALLED_APP and thus there was no error when you ran devserver. However, imports inside test.py will cause extra models of non-installed apps, if there is any, to be loaded and then collision will happen.

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

#2


0  

This was an issue with django-shop tests, as described in this comment: Django test fails when creating test database

这是django-shop测试的问题,如本评论中所述:Django测试在创建测试数据库时失败

I could find no way around it without changing the app's code, so I ended up just testing my own apps (running ./manage.py test myapp instead of ./manage.py test

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