4

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'
]
ShTurj
  • 41
  • 1
  • 4
  • Looks like the installed app should be `DoggieSitter.accounts` based on the error. How are you running the test and where from? – Iain Shelvington Apr 24 '22 at 10:49
  • I am running the command "python manage.py test" from the terminal – ShTurj Apr 24 '22 at 11:15
  • Have you tried adding `DoggieSitter.accounts` to your INSTALLED_APPS setting instead of `accounts`? – Iain Shelvington Apr 24 '22 at 11:23
  • Yes I did but there was an erro"ModuleNotFoundError: No module named 'DoggieSitter'" Moreover I was unable to upload the site add more issues unrelated to the testing – ShTurj Apr 24 '22 at 11:27
  • Hello @ShTurj try to add full app location eg. ***(accounts.apps.AccountsConfig)*** inside your INSTALLED_APPS – Ankit Tiwari Apr 24 '22 at 11:37
  • I am a bit new to the Django framework To add to the installed app Doggiesitter.accounts.Accountsconfig Where the Accounts is the class name? – ShTurj Apr 24 '22 at 11:44
  • Does this answer your question? [Django model "doesn't declare an explicit app\_label"](https://stackoverflow.com/questions/40206569/django-model-doesnt-declare-an-explicit-app-label) – Hamidreza Khorammfar Apr 24 '22 at 12:35
  • i tried and it didn't work to for me – ShTurj Apr 24 '22 at 15:41

1 Answers1

1

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.