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

6 2

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!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (1)

Collapse
 
asghara04 profile image
asghar ale • Edited

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?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay