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

后端 未结 3 2010
长发绾君心
长发绾君心 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: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.

提交回复
热议问题