Home > Software design >  ERRORS: store.Product.created_by: (fields.E301) Field defines a relation with the model 'auth.U
ERRORS: store.Product.created_by: (fields.E301) Field defines a relation with the model 'auth.U

Time:01-04

I am a beginner in Django. I am trying to build an app with user authentication. It was all ok but when I try to make a migration it occurs an error:

" ERRORS: store.Product.created_by: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out. HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'. "

I have tried other similar problems from this site but mine is not solved yet.

model.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django_countries.fields import CountryField
from django.utils.translation import gettext_lazy as _



class CustomAccountManager(BaseUserManager):
    
    # create super user
    def create_superuser(self, email, user_name, password, **others_fields):
        others_fields.setdefault('is_staff', True)
        others_fields.setdefault('is_superuser', True)
        others_fields.setdefault('is_active', True)

        if others_fields.get('is_staff') is not True:
            raise ValueError(
                'Superuser must be assigned to is_staff=True.'
            )
        if others_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.'
            )
        
        return self.create_user(email, user_name, password, **others_fields)

    # create user
    def create_user(self, email, user_name, password, **others_fields):
        if not email:
            raise ValueError(_('You must provide an email address.'))

        email = self.normalize_email(email)
        user = self.model(email = email, user_name=user_name, **others_fields)
        user.set_password(password)
        user.save()

        return user
    

class UserBase(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(('email address'),unique=True)
    user_name = models.CharField(max_length=250, unique=True)
    first_name = models.CharField(max_length=250, blank=True)
    about = models.TextField(_('about'), max_length=500, blank=True)

    country = CountryField()
    phone_nember = models.CharField(max_length=20, blank=True)
    postcoder = models.CharField(max_length=12, blank=True)
    adress_line_1 = models.CharField(max_length=150, blank=True)
    adress_line_2 = models.CharField(max_length=150, blank=True)
    town_city = models.CharField(max_length=150, blank=True)

    # user status
    is_active = models.BooleanField(default=False)
    is_staff = models.BooleanField(default= False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['user_name']

    class Meta:
        verbose_name = 'Accounts'
        verbose_name_plural = 'Accounts'
    
    def __str__(self):
        return self.user_name

settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # own
    'store',
    'basket',
    'account'
]

AUTH_USER_MODEL = 'account.UserBase'

CodePudding user response:

In your Product model, change your ForeignKey for User to UserBase instead of the default django auth User.

  • Related