Django迁移字符串引用的格式必须为appname.labelname

时间:2019-07-19 13:39:44

标签: python django python-3.x

当我像这样运行迁移时,

PS D:\pythonworkspace\WebLite> python manage.py makemigrations masterit

我收到以下错误消息,

PS D:\pythonworkspace\WebLite> python manage.py makemigrations masterit
Traceback (most recent call last):
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\utils.py", line 11, in make_model_tuple
    app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\makemigrations.py", line 170, in handle
    migration_name=self.migration_name,
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\autodetector.py", line 44, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\autodetector.py", line 128, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 214, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 271, in __init__
    self.render_multiple(list(models.values()) + self.real_models)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 306, in render_multiple
    model.render(self)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\state.py", line 574, in render
    return type(self.name, bases, body)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 139, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 305, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related.py", line 735, in contribute_to_class
    super().contribute_to_class(cls, name, private_only=private_only, **kwargs)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related.py", line 317, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related.py", line 79, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related.py", line 77, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "C:\Users\defuser\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\utils.py", line 20, in make_model_tuple
    "must be of the form 'app_label.ModelName'." % model
ValueError: Invalid model reference 'WebApp.Core.DjangoApps.commons.User'. String model references must be of the form 'app_label.ModelName'.

这是其他文件,

WebApp.Core.DjangoApps.commons 模型中,

class User(models.Model):
    user_name = models.CharField(max_length=100, unique=True)
    password = models.CharField(max_length=100)
    user_id = models.CharField(primary_key=True, max_length=255)
    user_department = models.CharField(max_length=100)
    email = models.CharField(max_length=70)

WebApp.Core.DjangoApps.Masterit 模型,

class Masterit(models.Model):
    item_id_itemcode = models.AutoField(primary_key=True)
    item_name = models.CharField(max_length=100)
    Bitattribute1 = models.BooleanField()
    Bitattribute2 = models.CharField(max_length=100)
    Bitattribute3 = models.CharField(max_length=100)
    Bitattribute4 = models.CharField(max_length=100)
    Bitattribute5 = models.CharField(max_length=100)  
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="im_created_by")
    created_date = models.DateField(auto_now_add=True)
    modified_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="im_modified_by")
    modified_date = models.DateField(auto_now=True)

Settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'WebApp.Core.DjangoApps.masterit.apps.masteritConfig',
    'WebApp.Core.DjangoApps.commons.apps.CommondataConfig',
]

以下错误是什么

ValueError: Invalid model reference 'WebApp.Core.DjangoApps.commons.User'. String model references must be of the form 'app_label.ModelName'.

app_label.ModelName形式必须是什么?我不明白,我看到了类似的问题must be of the form 'app_label.ModelName'." % model ValueError: Invalid model reference,但这无济于事,因为我的settings.py和AUTH_USER_MODEL中没有正确的位置配置文件。

如何解决此错误?请让我知道是否需要在这个问题上添加更多详细信息?

0 个答案:

没有答案