DEV Community

Cover image for Multiple User Types With Custom Data Fields for Django
Daniel Feldroy
Daniel Feldroy

Posted on • Originally published at youtu.be

Multiple User Types With Custom Data Fields for Django

How to add custom data fields to multiple user types with Django in a maintainable way that lets us easily use Django forms or DRF serializers. Features an exciting ending with a coffee cup!

Continues from where the "Multiple User Types | Django video finishes. Watch that video at on youtube.

Code from the video, with full source code available on github:

class SpyMore(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    gadgets = models.TextField()


class Spy(User):
    base_type = User.Types.SPY
    objects = SpyManager()

    class Meta:
        proxy = True

    def whisper(self):
        return "whisper"


class DriverMore(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    model = models.CharField(max_length=255)
    make = models.CharField(max_length=255)
    year = models.IntegerField()


class Driver(User):
    base_type = User.Types.DRIVER
    objects = DriverManager()

    @property
    def more(self):
        return self.drivermore

    class Meta:
        proxy = True

    def accelerate(self):
        return "Go faster"
Enter fullscreen mode Exit fullscreen mode

Tell me what intermediate-to-advanced topic you want me to cover next!

Discussion (1)

Collapse
asghara04 profile image
asghar ale • Edited on

hi sir i hope you be well, can you help me with my issue please, i building a project for jobs and hire and i want to companies user model and employ user model and i watcged the video it was really great information but i need to hve two specific database tables one for companies abd one for simple users(employers) how can i do that?