英文:
Using enums in Django with error ModuleNotFoundError: No module named 'models'
问题
我正在尝试在我的单元测试中使用枚举,但当我尝试导入它们时,我遇到了错误。
models.py
中的一部分代码如下:
class SeekingConstants:
MEN = 'Men'
WOMEN = 'Women'
BOTH_MEN_AND_WOMEN = 'Both men and women'
test_user_api.py
中的一部分代码如下:
from models import SeekingConstants
...
def test_update_user_seeking_choice(self):
"""Part 1: Update the seeking choice from nothing"""
payload = {
'email': 'test@example.com',
'seeking_choice': SeekingConstants.WOMEN
}
res = self.client.patch(ME_URL, payload)
self.user.refresh_from_db()
self.assertEqual(self.user.email, payload['email'])
self.assertTrue(self.user.seeking_choice, payload['seeking_choice'])
self.assertEqual(res.status_code, status.HTTP_200_OK)
"""Part 2: Update an existing seeking choice"""
new_payload = {
'email': 'test@example.com',
'seeking_choice': SeekingConstants.BOTH_MEN_AND_WOMEN
}
res = self.client.patch(ME_URL, new_payload)
self.user.refresh_from_db()
self.assertEqual(self.user.email, payload['email'])
self.assertTrue(self.user.seeking_choice, payload['seeking_choice'])
self.assertEqual(res.status_code, status.HTTP_200_OK)
我不确定为什么我不能导入这个枚举,或者我应该如何导入这个枚举。
英文:
I am trying to use enums in my unit tests but I'm getting an error when I try to import them.
An excerpt from models.py
:
class SeekingConstants:
MEN = 'Men'
WOMEN = 'Women'
BOTH_MEN_AND_WOMEN = 'Both men and women'
An excerpt from test_user_api.py
:
from models import SeekingConstants
...
def test_update_user_seeking_choice(self):
"""Part 1: Update the seeking choice from nothing"""
payload = {
'email': 'test@example.com',
'seeking_choice': SeekingConstants.WOMEN
}
res = self.client.patch(ME_URL, payload)
self.user.refresh_from_db()
self.assertEqual(self.user.email, payload['email'])
self.assertTrue(self.user.seeking_choice, payload['seeking_choice'])
self.assertEqual(res.status_code, status.HTTP_200_OK)
"""Part 2: Update an existing seeking choice"""
new_payload = {
'email': 'test@example.com',
'seeking_choice': SeekingConstants.BOTH_MEN_AND_WOMEN
}
res = self.client.patch(ME_URL, new_payload)
self.user.refresh_from_db()
self.assertEqual(self.user.email, payload['email'])
self.assertTrue(self.user.seeking_choice, payload['seeking_choice'])
self.assertEqual(res.status_code, status.HTTP_200_OK)
I'm not sure why I can't import this enum or how I should import this enum.
答案1
得分: 1
根据你的截图:
from ..models import SeekingConstants
英文:
according to your screenshot:
from ..models import SeekingConstants
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论