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

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

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 '管理员后台.产品配置.qiyun_admin_productconfig_cloudserver.HostType 

But, my HostType model path is this :
管理员后台.产品配置.qiyun_admin_productconfig_cloudserver.models.HostType.

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

My project directory is bellow:

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目录 sys.path.insert(0, BASE_DIR) sys.path.insert(0, os.path.join(PARENT_DIR,'旗云管理员后台')) sys.path.insert(0, os.path.join(PARENT_DIR,'用户前台')) sys.path.insert(0, os.path.join(PARENT_DIR,'用户管理员后台')) ...  INSTALLED_APPS = [     'django.contrib.admin',      ....     '旗云管理员后台.用户管理.qiyun_admin_useradminmanage',  #       '旗云管理员后台.用户管理.qiyun_admin_usergroups',  #        '旗云管理员后台.产品配置.qiyun_admin_productconfig_common', #       '旗云管理员后台.产品配置.qiyun_admin_productconfig_cloudserver',  #        '旗云管理员后台.财务管理.qiyun_admin_financialmanage_ordermanage', #      '旗云管理员后台.财务管理.qiyun_admin_financialmanage_financialmanage',  

EDIT

I have two Models(AvailableArea, AddressRegion) in the same models.py(旗云管理员后台.产品配置.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.


回答1:

The label of your app '旗云管理员后台.产品配置.qiyun_admin_productconfig_cloudserver' is 'qiyun_admin_productconfig_cloudserver' (the last component only).

When you use it to define a foreign key, you should use the label, not the full path of the app.

host_type = models.ForeignKey('qiyun_admin_productconfig_cloudserver.HostType', ...) 


回答2:

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' 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!