在Django中使用枚举时出现错误ModuleNotFoundError: No module named ‘models’

huangapple go评论64阅读模式
英文:

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)

在Django中使用枚举时出现错误ModuleNotFoundError: No module named ‘models’

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

huangapple
  • 本文由 发表于 2023年6月2日 04:57:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定