How to Implement multiple kinds of users in Django?

Viewed 140

I am new to Django so please bear with me if my questions seem too basic. So, I want to create a web app for a kind of a store in which I have three different kinds of users.

  1. Admin(Not Superuser) who can:
    • create, view, update, delete account for a Seller(agent)
    • issue them inventory
  2. Seller who can:
    • sell an inventory item to a Customer(customers cannot themselves purchase it, only the seller can do it by filling in a form)
    • a Customer account should automatically be created upon submission of the form by Seller or if the Customer already has an account, the purchase should be added to their account
  3. Customer
    • can login and view their account

What would be the best way to go about it? Using auth Groups, Profile models or anything else? Any help would be wonderful. If something is not very clear in the question, I can provide more details. Thanks.

1 Answers

Django already has a solution for this: a Group [Django-doc]. A user can belong to zero, one or more groups. A group can have zero, one or more Permissions [Django-doc].

These permissions can be defined by a Django model, for example for all models there are permissions, to view, add, change, and delete objects of a certain model, but you can define custom permissions as well, for example to visit a certain page. A user then has such permission if there is at least one group they are a member of that has such permission.

You can work for example with the @permission_required decorator [Django-doc], or the PermissionRequiredMixin [Django-doc] to enforce that only users that have the required permission(s) can see the given page.

You thus can make groups for a seller, customer, etc. Often people can have multiple roles, for exame being both a seller and a customer which thus is elegantly solved through the permission framework.

Related