Design Pattern Advices Requested


(Richard) #1

Hello guys,

I was reading the “Django Design Patterns and BestPractices”
and stopped on the chapter “Pattern — user profiles”.

Every website stores a different set of user profile details. However, Django’s built-in user model is meant for authentication details. The solution provided split the “Profile” in a BaseProfile, OrdinaryProfile, etc…
(I will try to upload the eBook here. See page 49 and follow…).

I’m just wondering to know how you personally feel with the solution provided?
To be honest, I’ve implemented it and I’m not really sure that’s the right way.

Of course, every applications have their own nuances…

Thanks for comments !

Regards,
RS


(Lúcio Henrique) #2

I could not see book, but I always leave “User Model” untouchable. If I need extra info or extra profiles, I create Profiles Models like you suggest. Here @vitor put some ways of extend user models. Take a look and check what best fits your needs.


(Richard) #3

Yes, you are right !

Here is the exemple provided :

class BaseProfile(models.Model):
      USER_TYPES = ( (0, 'Ordinary'), (1, 'SuperHero'),)
      user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True)
      user_type = models.IntegerField(max_length=1, null=True, choices=USER_TYPES)
      ...
      ...
     class Meta:
         abstract = True

class SuperHeroProfile(models.Model):
     origin = models.CharField(max_length=100, blank=True, null=True)
     class Meta:
         abstract = True

class OrdinaryProfile(models.Model):
     address = models.CharField(max_length=200, blank=True, null=True)
     class Meta:
         abstract = True

class Profile(SuperHeroProfile, OrdinaryProfile, BaseProfile):
    pass

@ll the best.


(Vitor Freitas) #4

I would say if you have some common details to all users, I would just extend the Django user model using the AbstractUser or AbstractBaseUser.

You can read on the link @lucheol shared.

Basically, extending from the AbstractUser means you are okay with all the fields Django provide and you just to add a few more. Extending from the AbstractBaseUser means you want to do a complete re-write of the User model, removing some fields, renaming others, and adding new fields.

Now, if your project has many user types, then you can read more about it here: