9

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

...

  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 348, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 85, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/fields/related.py", line 83, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/utils.py", line 23, in make_model_tuple
    "must be of the form 'app_label.ModelName'." % model
ValueError: Invalid model reference 'x.qiyun_admin_productconfig_cloudserver.HostType

But, my HostType model path is this :
x.qiyun_admin_productconfig_cloudserver.models.HostType.

The traceback less the .models in it. I don't know why.

Please PAY ATTENTION, the serializer and views(serializer view) is under the api directory.

and the settings:

...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PARENT_DIR = os.path.abspath(os.path.join(BASE_DIR, os.pardir))


sys.path.insert(0, BASE_DIR)
sys.path.insert(0, os.path.join(PARENT_DIR,'x'))
sys.path.insert(0, os.path.join(PARENT_DIR,'x'))
sys.path.insert(0, os.path.join(PARENT_DIR,'x'))
...

INSTALLED_APPS = [
    'django.contrib.admin',
     ....
    'x.qiyun_admin_useradminmanage',  #  
    'x.qiyun_admin_usergroups',  #  

    'x.qiyun_admin_productconfig_common', #  
    'x.qiyun_admin_productconfig_cloudserver',  #  

    'x.qiyun_admin_financialmanage_ordermanage', # 
    'x.qiyun_admin_financialmanage_financialmanage', 

EDIT

I have two Models(AvailableArea, AddressRegion) in the same models.py(x.qiyun_admin_productconfig_cloudserver.) :

class AvailableArea(models.Model):
    name = models.CharField(max_length=8)
    addressregion = models.ForeignKey(AddressRegion, default=1, related_name='availableareas', on_delete=models.CASCADE)

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

class AddressRegion(models.Model):
    name = models.CharField(max_length=8)

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

You see, should I still specified the addressregion = models.ForeignKey('qiyun_admin_productconfig_cloudserver.AddressRegion',...)?

And if other models if have ForeignKey refers to AddressRegion, I also imported it.


user7693832
  • 6,119
  • 19
  • 63
  • 114
  • @Alasdair See my Edit, my friend/. – user7693832 Nov 20 '17 at 13:29
  • @Alasdair My EDIT maybe let you know my question, and I just want to use more directory to group my apps, let my apps looks in good order, so I follow this post: https://stackoverflow.com/questions/47323242/can-i-use-a-directory-to-contains-a-couples-app-directory . Then I get my issues. – user7693832 Nov 20 '17 at 13:35

4 Answers4

31

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.

Yuriy Gyerts
  • 1,464
  • 18
  • 30
  • 2
    I've got the same error. My model lives under a User folder. App->users->models->models.py any indication about how to go this way? – HerbertRedford Apr 27 '20 at 00:32
5

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'
1

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.

David
  • 331
  • 2
  • 6
0

while creating the migration files: did you forget to delete the old migration files after renaming the model?

HoangYell
  • 4,100
  • 37
  • 31