Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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.


like image 395
user7693832 Avatar asked Nov 20 '17 13:11

user7693832


3 Answers

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.

like image 142
Yuriy Gyerts Avatar answered Oct 16 '22 02:10

Yuriy Gyerts


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'
like image 39
Salim Said Hemed Avatar answered Oct 16 '22 00:10

Salim Said Hemed


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.

like image 43
David Avatar answered Oct 16 '22 02:10

David