User0511
User0511

Reputation: 725

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

I'm trying to make django custom user model but I'm 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 used django 1.7. This is part of my setting.py and my app name is 'kerajinan':

AUTH_USER_MODEl = 'kerajinan.UserProfile'

model for user:

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 following error with relation in class product part of settings.AUTH_USER_MODEL:

  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?

Upvotes: 10

Views: 40821

Answers (1)

catavaran
catavaran

Reputation: 45575

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)

Upvotes: 25

Related Questions