Reputation: 222
I want to create multiple user types and roles depending on the user type while making the base user model abstract I tried something like this:-
class CustomAccountManager(BaseUserManager):
.....
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=150, unique=True)
email = models.EmailField(_('email address'), unique=True)
firstname = models.CharField(max_length=150, blank=True, null=True)
....
class Meta:
abstract = True
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'firstname']
objects = CustomAccountManager()
class Client(User):
location = models.BooleanField(_(""), default=True)
history = models.BooleanField(
_(""), default=True)
ip = models.BooleanField(_(""), default=True)
def __str__(self):
return self.username
class Vendor(User):
Storesmtn = models.ImageField(
upload_to="Images", max_length=100000, default="default/default.png")
Storesmtn = models.ImageField(
upload_to="Images", max_length=100000, default="default/default.png")
Storesmtn = models.CharField(max_length=220, blank=False, unique=True)
when I migrate the following error appears:-
users.Client.groups: (fields.E304) Reverse accessor for 'users.Client.groups' clashes with reverse accessor for 'users.Vendor.groups'.
HINT: Add or change a related_name argument to the definition for 'users.Client.groups' or 'users.Vendor.groups'.
users.Client.user_permissions: (fields.E304) Reverse accessor for 'users.Client.user_permissions' clashes with reverse accessor for 'users.Vendor.user_permissions'. HINT: Add or change a related_name argument to the definition for 'users.Client.user_permissions' or 'users.Vendor.user_permissions'.
users.Vendor.groups: (fields.E304) Reverse accessor for 'users.Vendor.groups' clashes with reverse accessor for 'users.Client.groups'.
HINT: Add or change a related_name argument to the definition for 'users.Vendor.groups' or 'users.Client.groups'.
users.Vendor.user_permissions: (fields.E304) Reverse accessor for 'users.Vendor.user_permissions' clashes with reverse accessor for 'users.Client.user_permissions'. HINT: Add or change a related_name argument to the definition for 'users.Vendor.user_permissions' or 'users.Client.user_permissions'.
Upvotes: 0
Views: 1037
Reputation: 535
This problem occurred due to the presence of fields related_name="user_set"
and related_query_name="user"
, Django doesn't have multiple users - it only has one user and then based on permissions users can do different things., though if developer decide to overwrite model should be change AUTH_USER_MODEL
variable at the Django setting.py and refer to inheritance model base on user model ,behind the scene, related name
follows the name of the new class.
If you want to customize user behavior in django, there are three things you can do:
Customize how you authenticate them. By default, authentication is done using a database where passwords are stored. You can authenticate against facebook/google etc. or against your existing user database - for example, with ActiveDirectory if you are on a Windows network.
Create custom permissions, and based on these permissions, restrict what functions users can execute. By default, on every model - django will add basic permissions "can edit", "can delete", "can read". You can create your own and then check if the user has these specific permissions.
You can store extra information about the user, along with whatever normally is stored by django. There are two ways to do this, depending on how much customization you need. If everything django provides by default works for you, and all you want to do is store extra information about the user you can extend the user model - in previous versions this was called creating a custom profile. The other option you have is to create your own User model, if you want deeper customization. The most common use of a custom user model is if you want to use an email address as the username.
The way I recommend in your case, since all you want to do is store extra information about a user, define permission for client and vendor and with create Client
and Vendor
as a proxy model to separate them when using.
Upvotes: 1