python - Best way to handle multiple type of users in Django -
in project have deal different type of users e.g. costumers , employers. each type of user have own fields , permissions: costumer can buy things whereas employer can not.
i have read django docs , looks there 2 options:
- costumize
abstractuserclass , adding all fields of costumers , employers. use permission system grant/revoke permission or create group each type of user. downside here there unused fields. adopt proxy model:
case seems more reasonable don't know how deal permissions. consider i'd usedjango.contrib.auth.models import user django.db import modelsclass costumer(models.model): user = models.onetoonefield(user, on_delete=models.cascade)
class meta: db_table = 'costumers' permissions = ( ("access_this", "user may access this"), ) ordering = []class employee(models.model): user = models.onetoonefield(user, on_delete=models.cascade)
class meta: db_table = 'employers' permissions = ( ("access_that", "user may access that"), ) ordering = []@permission_requiredinstead of checking type (if user has specific field) because seems more legit django system.
so in end, best way approach such scenario?
the first solution better.
the downside here there unused fields.
i disagree this, don't have store fields within user model. also, if you're talking 5 fields example, doesn't matter.
you can extend abtractuser , use composition; don't have put fields there:
class user(abstractuser): email = ... ... # employer, let's want save company details company = models.foreignkey('myapp.company', null=true, blank=true) ... # customer customer_details = models.foreignkey('...') this way record user_type if want or deduce type foreign key (if there company, it's employer).
to more model, need know differentiate employer customer in application. note solution, instance of user both.
concerning permissions, feel it's separate problem; i'd recommend sort last. if design pick close reality , features working; adding custom permissions easy.
Comments
Post a Comment