简体   繁体   English

“电子邮件”字段与 model“account.account”中的“电子邮件”字段冲突。 Django3.2

[英]The field 'email' clashes with the field 'email' from model 'account.account'. Django3.2

Newbie in django here. django 的新手在这里。 I've been trying to create a simple site with django and I just finished creating the models.我一直在尝试使用 django 创建一个简单的网站,我刚刚完成了模型的创建。 However, when I try to makemigrations ,I get this:但是,当我尝试makemigrations时,我得到了这个:

SystemCheckError: System check identified some issues:

ERRORS:
account.Account.email: (models.E006) The field 'email' clashes with the field 'email' from model 'account.account'.

I checked my code, but i couldn't find anything bad.我检查了我的代码,但我找不到任何不好的地方。 Maybe my eyes are broken.也许我的眼睛坏了。

Here's my models.py:这是我的模型.py:

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

class AccountManager(BaseUserManager):
    def create_user(self, first_name, last_name, name, email, password=None):
        if not email:
            raise ValueError("User must have an email address")
        
        user = self.model(
            email = self.normalize_email(email),
            first_name = first_name,
            last_name = last_name,
            name = name,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_superuser(self, first_name, last_name, name, email, password=None):
        user = self.create_user(
            email = self.normalize_email(email),
            password = password,
            first_name = first_name,
            last_name = last_name,
            name = name,
        )
        user.is_admin = True
        user.is_staff = True
        user.is_active = True
        user.is_superadmin = True
        user.save(using=self._db)
        return user
    
class Account(AbstractBaseUser):
    first_name      = models.CharField(max_length=50)
    last_name       = models.CharField(max_length=50)
    email           = models.EmailField(max_length=100, unique=True)
    phone_number    = models.CharField(max_length=50)
    name            = models.CharField(max_length=50)
    reputation      = models.BigIntegerField(default=1)
    downvote_count  = models.IntegerField(default=0)
    # mandatory fields
    created_date    = models.DateTimeField(auto_now_add=True)
    last_login      = models.DateTimeField(auto_now_add=True)
    is_admin        = models.BooleanField(default=False)
    is_staff        = models.BooleanField(default=False)
    is_active       = models.BooleanField(default=False)
    is_superadmin   = models.BooleanField(default=False)

    USERNAME_FIELD = email
    REQUIRED_FIELDS = ["name", "first_name", "last_name"]
    
    objects = AccountManager()

    def full_name(self):
        return f"{self.first_name} {self.last_name}"
    
    def __str__(self):
        return self.email
    
    def has_perm(self):
        return self.is_admin
    

class Address(models.Model):
    user            = models.ForeignKey(Account, on_delete=models.CASCADE)
    country         = models.CharField(max_length=100)
    state           = models.CharField(max_length=50)
    city            = models.CharField(max_length=50)
    zipcode4        = models.IntegerField()
    line_1          = models.CharField(max_length=200)
    line_2          = models.CharField(max_length=200)
    whole_address   = models.CharField(max_length=1024)
    created_date    = models.DateTimeField(auto_now_add=True)
    updated_date    = models.DateTimeField(auto_now=True)

I'm not sure if you need it, but here's my admin.py:我不确定你是否需要它,但这是我的 admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import Account, Address

# Register your models here.
class AccountAdmin(UserAdmin):
    list_display = ["email", "first_name", "last_name", "name", "last_login", "created_date", "is_active"]
    list_display_links = ["email", "first_name", "last_name"]
    readonly_fields = ["last_login", "created_date"]
    ordering = ["-created_date"]
    filter_horizontal = ()
    list_filter = ()
    fieldsets = ()

class AddressAdmin(admin.ModelAdmin):
    list_display = ["user", "whole_address"]
    filter_horizontal = ()
    list_filter = ()
    fieldsets = ()

admin.site.register(Account, AccountAdmin)
admin.site.register(Address, AddressAdmin)

As @Willem Van Onsem says in the comment, you should this like this:正如@Willem Van Onsem 在评论中所说,你应该这样:

USERNAME_FIELD = "email"

In your create_superuser function, change email = self.normalize_email(email) to email在您的create_superuser function 中,将email = self.normalize_email(email)更改为email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM