django.core.exceptions.FieldError:未知字段

时间:2018-09-20 17:51:29

标签: python django

我得到了

django.core.exceptions.FieldError: Unknown field(s) (authorized_users_read_or_write, authorized_users_to_write, authorized_users_to_read) specified for FileUpload
更改模型中3个字段的名称后,运行makemigrations

。我希望Django接受名称更改并根据更改进行迁移。该模型是:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

class FileUpload(models.Model):
    name = models.CharField(max_length=255, default='')
    file = models.FileField(upload_to='uploads/%Y/%m/%d/')
    upload_time = models.DateTimeField(auto_now_add=True, blank=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='files_owned')
    description = models.TextField(null=True, blank=True)
    users_authorized_to_read = models.ManyToManyField(User, related_name='file_read_permissions', null=True, blank=True)
    users_authorized_to_write = models.ManyToManyField(User, related_name='file_write_permissions', null=True, blank=True)
    users_authorized_to_read_or_write = models.ManyToManyField(User, related_name='file_read_write_permissions', null=True, blank=True)

    def __str__(self):
        return self.name

我更改了底部3个字段,这就是我所做的全部更改。

$ python3 manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 350, in execute
    self.check()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/cchilders/projects/secure_code_warrior_django_project/secure_code_warrior_django_project/urls.py", line 24, in <module>
    url(r'file-manager/', include('file_management.urls')),
  File "/usr/local/lib/python3.5/dist-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/cchilders/projects/secure_code_warrior_django_project/file_management/urls.py", line 3, in <module>
    from . import views
  File "/home/cchilders/projects/secure_code_warrior_django_project/file_management/views.py", line 9, in <module>
    from .forms import FileUploadForm
  File "/home/cchilders/projects/secure_code_warrior_django_project/file_management/forms.py", line 26, in <module>
    class FileUploadForm(ModelForm):
  File "/usr/local/lib/python3.5/dist-packages/django/forms/models.py", line 266, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (authorized_users_read_or_write, authorized_users_to_write, authorized_users_to_read) specified for FileUpload

1 个答案:

答案 0 :(得分:0)

一个可能导致此错误的问题是您在ModelForm中使用了旧的字段名,