RuntimeError:Model do do do do do app_label和installed_apps中的应用

发布于 2025-01-23 18:12:45 字数 945 浏览 2 评论 0原文

我正在Django编写一个应用程序,我正在尝试进行一些单元测试 但是我似乎找不到为什么测试失败 那是测试页面:

import re
from django.test import TestCase
from django.urls import reverse
from . import models



class BasicTests(TestCase):

    def test_firstname(self):
        print('test11')
        acc = models.Accounts()
        acc.first_name = 'Moran'
        self.assertTrue(len(acc.id) <= 9, 'Check name is less than 50 digits long')
        self.assertFalse(len(acc.id) > 50, 'Check name is less than 50 digits long')

我遇到的错误是:

RuntimeError:模型类doggiesitter.accounts.models.accounts 不会声明明确的app_label,也不在 installed_apps

就是我安装的应用程序:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts'
]

I am writing an app in Django and I'm trying to do some unit testing
but I can't seem to find why the test is failing
that is the test page:

import re
from django.test import TestCase
from django.urls import reverse
from . import models



class BasicTests(TestCase):

    def test_firstname(self):
        print('test11')
        acc = models.Accounts()
        acc.first_name = 'Moran'
        self.assertTrue(len(acc.id) <= 9, 'Check name is less than 50 digits long')
        self.assertFalse(len(acc.id) > 50, 'Check name is less than 50 digits long')

the error i get is :

RuntimeError: Model class DoggieSitter.accounts.models.Accounts
doesn't declare an explicit app_label and isn't in an application in
INSTALLED_APPS

thats my installed app:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounts'
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2025-01-30 18:12:45

尝试将上面的第4行更改为明确导入,例如从doggiesitter.accounts导入模型

我都会在运行tests.py的测试时都有此问题。 >来自.models导入modelName 。搜索了一个小时左右后,我偶然发现了这个答案是对我的教程的确切答案。 >。

就我而言,我正在尝试。我的项目结构如下,因此我从apps.recipes.models导入食谱更改为,并且测试现在运行良好。这很可惜,因为我宁愿继续使用相对进口。

src
├── __init__.py
├── apps
│   ├── accounts
│   ├── core
│   └── recipes
│   │   ├── models.py
│   │   ├── ... etc
├── config
│   ├── __init__.py
│   ├── admin.py
│   ├── asgi.py
│   ├── db.sqlite3
│   ├── secrets
│   ├── settings
│   │   ├── __init__.py
│   │   ├── base
│   │   └── development
│   ├── tests.py
│   ├── urls.py
│   ├── utilities.py
│   └── wsgi.py
├── manage.py
├── static
└── templates

PS-似乎也起作用的另一种更明确的方法是:

from django.apps import apps
Recipe = apps.get_model(app_label='recipes', model_name='Recipe')

...但是我认为我更喜欢简单的显式导入语句。

Try changing line 4 above to an explicit import such as from DoggieSitter.accounts import models

I had this problem whenever running tests where the tests.py had a relative import such from .models import ModelName. After searching for an hour or so, I stumbled on this answer to exactly the tutorial I was following.

In my case, I was trying from .models import Recipe. My project structure is as below, so I changed to from apps.recipes.models import Recipe and the test now runs fine. It's a shame because I'd prefer to keep using relative imports.

src
├── __init__.py
├── apps
│   ├── accounts
│   ├── core
│   └── recipes
│   │   ├── models.py
│   │   ├── ... etc
├── config
│   ├── __init__.py
│   ├── admin.py
│   ├── asgi.py
│   ├── db.sqlite3
│   ├── secrets
│   ├── settings
│   │   ├── __init__.py
│   │   ├── base
│   │   └── development
│   ├── tests.py
│   ├── urls.py
│   ├── utilities.py
│   └── wsgi.py
├── manage.py
├── static
└── templates

PS - another even more explicit way that also seems to work is:

from django.apps import apps
Recipe = apps.get_model(app_label='recipes', model_name='Recipe')

... but I think I prefer the simpler explicit import statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文