0

django的新手在这里。我一直在尝试用 django 创建一个简单的站点,而我刚刚完成了模型的创建。但是,当我尝试时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'.

我检查了我的代码,但我找不到任何不好的地方。也许我的眼睛坏了。

这是我的models.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)

我不确定你是否需要它,但这是我的 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)

4

2 回答 2

0

正如@Willem Van Onsem 在评论中所说,你应该这样:

USERNAME_FIELD = "email"
于 2021-10-09T14:51:53.087 回答
0

在您的create_superuser功能中,更改email = self.normalize_email(email)email

于 2021-10-09T14:55:10.633 回答