[Fixed]-Extending Django User with subclasses

1šŸ‘

Iā€™m on mobile, so I canā€™t test this, butā€¦

I would set a ā€œrelated_nameā€ on your CommonUser.user field called ā€œcommonuserā€. Then you can just use:

request.user.commonuser.photo

like you are already.

I think the issue is that you are referencing a Django User object to reference a backwards relationship without the proper name.

šŸ‘¤PANDA Stack

0šŸ‘

First off, I think this model is more of a Profile than User. If you donā€™t mind using 1.9 (and postgres) then this is a perfect usecase for a JSON field. You can filter with regular lookups and donā€™t need to specify each type. That way you can also extend the user model in such a way that a user can fulfill many roles at once. The other thing I thought of was linking it the other way around:

class UserProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    # ...

class Installer(models.Model):
    profile = models.ForeignKey(UserProfile, related_name='installer')
    #HERE: Specific properties for Installer Companies


class Administration(models.Model):
    profile = models.ForeignKey(UserProfile, related_name='admin')
    #HERE: Specific properties for Administration


class Client(models.Model):
    profile = models.ForeignKey(UserProfile, related_name='client')
    #HERE: Specific properties for Clients

Leave a comment