[Answered ]-ERRORS: inside.UserProfile.user: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out

1👍

You are defining a relationship to the default User Model, which you are not using anymore as you have created a custom user model. Remove the one_to_one_field to avoid this error
And to further errors you have to create a custom manager

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractBaseUser
from django.conf import settings

from django_countries.fields import CountryField

# Create your models here.

class UserProfile(AbstractBaseUser):
    # Remove this as you no longer using the default User Model.
    # user = models.OneToOneField(User, on_delete = models.DO_NOTHING)
    phone_number = models.CharField(max_length = 16, unique = True, blank = False, null = False)
    country = CountryField()
    # As you are using UID as your username field 
    # it is not safe to make it blank and null true 
    uid = models.UUIDField(
        default = None,
        blank = True,
        null = True,
        unique = True,
    )
    USERNAME_FIELD = "uid"
    REQUIRED_FIELDS = ['phone_number', 'country']
    # Create your custom user manager 
    objects = YOUR_CUSTOM_USER_MANAGER()

Note: As you are using uid as your username field, you have to write a custom authentication backend so that you can use the phone number to authenticate the user, otherwise you have to use the phone number as a username field if you don’t want to create a custom authentication backend.

Leave a comment