django中的未知字段错误

时间:2018-01-30 15:59:11

标签: python django django-models

我正在尝试按照教程操作,当我运行makemigrations时出现此错误,但是在教程中,他们没有得到错误。

这是教程:https://www.codingforentrepreneurs.com/blog/how-to-create-a-custom-django-user-model/

这是我在视频中24:59得到的错误: django.core.exceptions.FieldError:为CustomUser指定的未知字段(用户名)。这是完整的追溯:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/base.py", line 327, in execute
    self.check()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/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 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/test/urls.py", line 27, in
 <module>
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/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 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/urls.py", line 4,
 in <module>
    from . import views
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/views.py", line 5
, in <module>
    from . import forms
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/forms.py", line 6
, in <module>
    class UserCreateForm(UserCreationForm):
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/dj
ango/forms/models.py", line 262, in __new__
    raise FieldError(message)
(simplesocialenv) chrismaltez@Chriss-MacBook-Pro:~/Desktop/pycharmprojects/UDEMY/test$ python manage.py makemigrationsesocialenv)
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/base.py", line 327, in execute
    self.check()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/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 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/test/urls.py", line 27, in <module>
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/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 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/urls.py", line 4, in <module>
    from . import views
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/views.py", line 5, in <module>
    from . import forms
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/test/accounts/forms.py", line 6, in <module>
    class UserCreateForm(UserCreationForm):
  File "/Users/chrismaltez/anaconda3/envs/simplesocialenv/lib/python3.5/site-packages/django/forms/models.py", line 262, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (username) specified for CustomUser

以下是我模特中的相关要点:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager


# Create your models here.

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, is_active=True, is_staff=False, is_admin=False):
        if not email:
            raise ValueError("Users must have an email address")
        if not password:
            raise ValueError('Users must have a password')

        user_obj = self.model(
            email=self.normalize_email(email)
        )
        user_obj.set_password(password)  # change user password
        user_obj.staff = is_staff
        user_obj.admin = is_admin
        user_obj.active = is_active
        user_obj.save(using=self._db)
        return user_obj

    def create_staffuser(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_staff=True,
        )
        return user

    def create_superuser(self, email, password=None):
        user = self.create_user(
            email,
            password=password,
            is_staff=True,
            is_admin=True
        )
        return user

class CustomUser(AbstractBaseUser):
    email = models.EmailField(max_length=255, unique=True)
    # full_name = models.CharField(max_length=255, blank=True, null=True)
    is_active = models.BooleanField(default=True)  #can login
    is_staff = models.BooleanField(default=False)  # staff user non superuser
    is_admin = models.BooleanField(default=False)  # superuser
    timestamp = models.DateTimeField(auto_now_add=True)

    # confirm = models.BooleanField(default=False)
    # confirmed_date = models.DateTimeField(default=False)

    USERNAME_FIELD = 'email'

    #USERNAME_FIELD and password are required by default
    REQUIRED_FIELDS = [] #['full_name'] #python manage.py createsuperuser

    objects = UserManager()

    def __str__(self):
        return self.email

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    @property
    def is_staff(self):
        return self.is_staff

    @property
    def is_admin(self):
        return self.is_admin

    @property
    def is_active(self):
        return self.is_active

class Profile(models.Model):
    user = models.OneToOneField(CustomUser)
    #location
    #bio
    #experience
    #profile pic




class GuestEmail(models.Model):
    email = models.EmailField()
    active = models.BooleanField(default=True)
    update = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.email

我的forms.py文件:

from django.contrib.auth import get_user_model
#^returns user model that is currently active in this project
from django.contrib.auth.forms import UserCreationForm


class UserCreateForm(UserCreationForm):
    #^make sure that class name is not same name as import
    class Meta:
        fields = ('username', 'email', 'password1', 'password2')
        model = get_user_model()
        #^when users sign up, this is what they will have access to


    def __init__(self,*args,**kwargs):
        super().__init__(*args, **kwargs)
        self.fields['username'].label = 'Display Name'
        #^if project was twitter,
        self.fields['email'].label = "Email Address"

我在settings.py文件中有这个:

AUTH_USER_MODEL = 'accounts.CustomUser'  # changes the built-in user model to ours

感谢任何建议或指示!

1 个答案:

答案 0 :(得分:0)

可能在CustomUser您正在使用的用户名字段中,您在模型中没有{{1}}