Django model “doesn't declare an explicit app_label”

后端 未结 28 1762
無奈伤痛
無奈伤痛 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 16:16

    I got this error on importing models in tests, i.e. given this Django project structure:

    |-- myproject
        |-- manage.py
        |-- myproject
        |-- myapp
            |-- models.py  # defines model: MyModel
            |-- tests
                |-- test_models.py
    

    in file test_models.py I imported MyModel in this way:

    from models import MyModel
    

    The problem was fixed if it is imported in this way:

    from myapp.models import MyModel
    

    Hope this helps!

    PS: Maybe this is a bit late, but I not found in others answers how to solve this problem in my code and I want to share my solution.

    0 讨论(0)
  • 2020-11-27 16:17

    If you have got all the config right, it might just be an import mess. keep an eye on how you are importing the offending model.

    The following won't work from .models import Business. Use full import path instead: from myapp.models import Business

    0 讨论(0)
  • 2020-11-27 16:17

    The issue is that:

    1. You have made modifications to your models file, but not addedd them yet to the DB, but you are trying to run Python manage.py runserver.

    2. Run Python manage.py makemigrations

    3. Python manage.py migrate

    4. Now Python manage.py runserver and all should be fine.

    0 讨论(0)
  • 2020-11-27 16:19

    I got this one when I used ./manage.py shell then I accidentally imported from the root project level directory

    # don't do this
    from project.someapp.someModule import something_using_a_model
    # do this
    from someapp.someModule import something_using_a_model
    
    something_using_a_model()
    
    0 讨论(0)
提交回复
热议问题