pythondjangodjango-modelsdjango-databasedjango-custom-user

Django (proxy and abstract) add extra fields to child of a custom-user class (AbstrsactBaseUser)


I created a custom user class that is inherited from AbstractBaseUser, this class has a chil class. The problem is I can't add new fields to the children's classes because they had (class Meta: proxy = Ture). The following error appears: ?: (models.E017) Proxy model 'StudentUser' contains model fields.


user_model.py

class AppUser(AbstractBaseUser, PermissionsMixin):
    objects = MyUserManager()

    username = models.CharField(max_length=128)
    email = models.EmailField(max_length=64, unique=True)
        .
        .
        .


class StudentUser(AppUser):
    objects = StudentManager()

    GPA = models.DecimalField(max_digits=4,decimal_places=2) #This causes the error if proxy=True 

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        self.password = make_password(self.password)
        return super().save(*args, **kwargs)

sittings.py

INSTALLED_APPS = [
   .
   .
   .
    "app",
]
AUTH_USER_MODEL = "app.AppUser

admin.py

admin.site.register(AppUser)
admin.site.register(StudentUser)

When I remove (proxy = True) from the child and add (abstract = True) to the parent the following error appears: django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.AppUser' that has not been installed

user_model.py

class AppUser(AbstractBaseUser, PermissionsMixin):
    objects = MyUserManager()

    class Meta:
         abstract = True

    username = models.CharField(max_length=128)
    email = models.EmailField(max_length=64, unique=True)
        .
        .
        .


class StudentUser(AppUser):
    objects = StudentManager()

    GPA = models.DecimalField(max_digits=4,decimal_places=2) 

    def save(self, *args, **kwargs):
        self.password = make_password(self.password)
        return super().save(*args, **kwargs)

How can I extend the AppUser and add fields to the child and keep the AppUser as the user model AUTH_USER_MODEL = "app.AppUser ?


Solution

  • It worked, I deleted the class Meta for both proxy and abstract:

    user_model.py

    class AppUser(AbstractBaseUser, PermissionsMixin):
        objects = MyUserManager()
    
        username = models.CharField(max_length=128)
        email = models.EmailField(max_length=64, unique=True)
            .
            .
    
    
    class StudentUser(AppUser):
        objects = StudentManager()
    
        GPA = models.DecimalField(max_digits=4,decimal_places=2) 
    
        def save(self, *args, **kwargs):
            self.password = make_password(self.password)
            return super().save(*args, **kwargs)