Django model “doesn't declare an explicit app_label”

后端 未结 28 1761
無奈伤痛
無奈伤痛 2020-11-27 15:38

I\'m at wit\'s end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:

Model class django.contrib.co         


        
相关标签:
28条回答
  • 2020-11-27 15:53

    as a noob using Python3 ,I find it might be an import error instead of a Django error

    wrong:

    from someModule import someClass
    

    right:

    from .someModule import someClass
    

    this happens a few days ago but I really can't reproduce it...I think only people new to Django may encounter this.here's what I remember:

    try to register a model in admin.py:

    from django.contrib import admin
    from user import User
    admin.site.register(User)
    

    try to run server, error looks like this

    some lines...
    File "/path/to/admin.py" ,line 6
    tell you there is an import error
    some lines...
    Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label
    

    change user to .user ,problem solved

    0 讨论(0)
  • 2020-11-27 15:54

    For PyCharm users: I had an error using not "clean" project structure.

    Was:

    project_root_directory
    └── src
        ├── chat
        │   ├── migrations
        │   └── templates
        ├── django_channels
        └── templates
    

    Now:

    project_root_directory
    ├── chat
    │   ├── migrations
    │   └── templates
    │       └── chat
    ├── django_channels
    └── templates
    

    Here is a lot of good solutions, but I think, first of all, you should clean your project structure or tune PyCharm Django settings before setting DJANGO_SETTINGS_MODULE variables and so on.

    Hope it'll help someone. Cheers.

    0 讨论(0)
  • 2020-11-27 15:56

    I've got a similar error while building an API in Django rest_framework.

    RuntimeError: Model class apps.core.models.University doesn't declare an explicit > app_label and isn't in an application in INSTALLED_APPS.

    luke_aus's answer helped me by correcting my urls.py

    from

    from project.apps.views import SurgeryView
    

    to

    from apps.views import SurgeryView
    
    0 讨论(0)
  • 2020-11-27 15:57

    After keep on running into this issue and keep on coming back to this question I thought I'd share what my problem was.

    Everything that @Xeberdee is correct so follow that and see if that solves the issue, if not this was my issue:

    In my apps.py this is what I had:

    class AlgoExplainedConfig(AppConfig):
        name = 'algo_explained'
        verbose_name = "Explain_Algo"
        ....
    

    And all I did was I added the project name in front of my app name like this:

    class AlgoExplainedConfig(AppConfig):
    name = '**algorithms_explained**.algo_explained'
    verbose_name = "Explain_Algo"
    

    and that solved my problem and I was able to run the makemigrations and migrate command after that! good luck

    0 讨论(0)
  • 2020-11-27 15:58

    I got this error while trying to upgrade my Django Rest Framework app to DRF 3.6.3 and Django 1.11.1.

    For anyone else in this situation, I found my solution in a GitHub issue, which was to unset the UNAUTHENTICATED_USER setting in the DRF settings:

    # webapp/settings.py
    ...
    REST_FRAMEWORK = {
        ...
        'UNAUTHENTICATED_USER': None
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 16:00

    Most probably you have dependent imports.

    In my case I used a serializer class as a parameter in my model, and the serializer class was using this model: serializer_class = AccountSerializer

    from ..api.serializers import AccountSerializer
    
    class Account(AbstractBaseUser):
        serializer_class = AccountSerializer
        ...
    

    And in the "serializers" file:

    from ..models import Account
    
    class AccountSerializer(serializers.ModelSerializer):
        class Meta:
            model = Account
            fields = (
                'id', 'email', 'date_created', 'date_modified',
                'firstname', 'lastname', 'password', 'confirm_password')
        ...
    
    0 讨论(0)
提交回复
热议问题