Newbie in django here. I've been trying to create a simple site with django and I just finished creating the models. However, when I try to makemigrations
,I get this:
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:
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:
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)
CodePudding user response:
As @Willem Van Onsem says in the comment, you should this like this:
USERNAME_FIELD = "email"
CodePudding user response:
In your create_superuser
function, change email = self.normalize_email(email)
to email