I am trying to make my django app require a unique email address when creating the account. When I added (the commented line in the models) the unique email constraint in the model I used to add fields before it just adds another email field instead of altering the original one.
So after having found another solution with extending the AbstractUser class unfortunately Im getting 'the model User is not registered' error when im trying to make migrations.
Please advice me on what would be a way to go to have both extra fields as well as unique email constraint.
Heres how i have extra fields added to the user model:
models
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
points = models.IntegerField(default=0)
#### email = models.EmailField(("email address"), blank=True, null=True, unique=True)
profession = models.CharField(max_length=30, blank=True, null=True,
choices =[('a', 'a'), ('b', 'b')])
def __str__(self):
return self.user.username
admin
from django.contrib import admin
from account.models import Account
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
class AccountInline(admin.StackedInline):
model = Account
can_delete = False
verbose_name_plural = 'Accounts'
class CustomizedUserAdmin (UserAdmin):
inlines = (AccountInline, )
admin.site.unregister(User)
admin.site.register(User, CustomizedUserAdmin)
It worked until ive added:
models
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
email = models.EmailField(unique=True)
settings
AUTH_USER_MODEL = 'account.CustomUser'
You should use one of the user models not both whether to use "User" or "CustomUser".
So, in your models:
In your admin do the same.