must be of the form 'app_label.ModelName'." % model ValueError: Invalid model reference

后端 未结 3 1997
长发绾君心
长发绾君心 2021-01-14 17:40

When I python3 manage.py makemigrations, I get bellow error:

...

  File \"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-         


        
相关标签:
3条回答
  • 2021-01-14 17:41

    Has a bit different situation with the same error message:

    ValueError: Invalid model reference 'users.models.MyUser'. String model references must be of the form 'app_label.ModelName'.

    The error was that I've specified models in the path to the MyUser model:

    AUTH_USER_MODEL = 'users.models.MyUser'
    

    But we shouldn't do it, we just need specify the package and model name only

    AUTH_USER_MODEL = 'users.MyUser'
    

    And error is gone.

    0 讨论(0)
  • 2021-01-14 17:42

    I had the same issue but resolved it by adding my python package to INSTALLED_APPS like so:

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'scm',
    'scm.staff'
    

    ] the Package in question is scm.staff

    and then specifying the models as such

    AUTH_USER_MODEL='staff.Staff'
    
    0 讨论(0)
  • 2021-01-14 17:51

    I also ran into this same error. In my case, I noticed that when I refactored the name of a model project-wide, it change a reference in such a way that does not work.

    I'm cutting out some irrelevant code, but the original code looked like (note this is within an app named "blog"):

    #model
    class Category(models.Model):
        pass
    
    class Post(models.Model):
        categories = models.ManyToManyField('blog.Category', related_name='posts')
    

    The I chose to refactor Category to Categorie to test something on another page, and it changed the line

    categories = models.ManyToManyField('blog.Category', related_name='posts')
    

    to

    categories = models.ManyToManyField('blog.models.Categorie', related_name='posts')
    

    Which is in conflict with how django likes things, as stated in the error message

    String model references must be of the form 'app_label.ModelName'
    

    This was the only place I could find an issue, but it looks like the IDE (PyCharm) was trying to be helpful and created an error.

    0 讨论(0)
提交回复
热议问题