Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.7 - Add or change a related_name argument to the definition for

Tags:

python

django

hello i trying to make django custom user model but i am getting error like this

    auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserProfile.groups'.
    HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserProfile.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserProfile.user_permissions'.
kerajinan.Product.author: (fields.E300) Field defines a relation with model 'settings.AUTH_USER_MODEL', which is either not installed, or is abstract.
kerajinan.UserProfile.groups: (fields.E304) Reverse accessor for 'UserProfile.groups' clashes with reverse accessor for 'User.groups'.
    HINT: Add or change a related_name argument to the definition for 'UserProfile.groups' or 'User.groups'.
kerajinan.UserProfile.user_permissions: (fields.E304) Reverse accessor for 'UserProfile.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
    HINT: Add or change a related_name argument to the definition for 'UserProfile.user_permissions' or 'User.user_permissions'.

i use django 1.7 this is part of setting.py and my name apps is 'kerajinan:

AUTH_USER_MODEl = 'kerajinan.UserProfile'

model.py:

class Product(models.Model):
author          = models.ForeignKey('settings.AUTH_USER_MODEL')
userprofile     = models.ForeignKey('UserProfile')
title           = models.CharField(max_length=50)

def __str__(self):
    return self.title;
class MyUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not email:
            raise ValueError('Users must have an email address')
        if not username:
            raise ValueError('Users must have a username')

        user = self.model(
            username = username,
            email    = self.normalize_email(email),
        )
        user.is_active  = True
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, password):
        user = self.create_user(username=username,email=email, password=password)

        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user 

class UserProfile(AbstractBaseUser, PermissionsMixin):

    alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed.')
    username    = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
    email       = models.EmailField(verbose_name='email address', unique=True, max_length=244)
    first_name  = models.CharField(max_length=30, null=True, blank=True)
    last_name   = models.CharField(max_length=50, null=True, blank=True)
    is_active   = models.BooleanField(default=True, null=False)
    is_staff    = models.BooleanField(default=False, null=False)


    # profile_image = models.ImageField(upload_to="uploads", blank=False, null=False, default="/static/images/defaultuserimage.jpg")

    objects = MyUserManager()

    USERNAME_FIELD  = 'email'
    REQUIRED_FIELDS = ['username']

    def get_full_name(self):
        fullname = self.first_name+" "+self.last_name
        return self.fullname

    def get_short_name(self):
        return self.username

    def __str__(self):
        return self.email

i am getting error with relation in class product part of settings.AUTH_USER_MODEL this is error:

  ERRORS:
kerajinan.Product.author: (fields.E300) Field defines a relation with model 'settings.AUTH_USER_MODEL', which is either not installed, or is abstract.

can you help me solve this error?

like image 212
User0511 Avatar asked Dec 02 '22 14:12

User0511


1 Answers

You use the lowercase l in the setting name. Python variables are case-sensitive so change it to uppercase L:

AUTH_USER_MODEL = 'kerajinan.UserProfile'

UPDATE: To create relation to custom user model you should remove the quotes in the FK definition:

author = models.ForeignKey(settings.AUTH_USER_MODEL)
like image 118
catavaran Avatar answered Dec 06 '22 10:12

catavaran