Hi.. I was given this description on which i have to design model
- You have to design a user model in Django. You need to consider following use cases
Organization has multiple user type
- Organization's own employees
- Brands which do marketing on organization
- Developers
- End user.
- Now each of above users is firstly a organization's user. There are hierarchy in the brand (Every level has a different permission level)
- Owner
- Manager
- Associate
- There are hierarchy in Organization itself organisation (Every level has a different permission level)
- CEO
- VP
- Director
- Moderator
- Consider these cases you have to design database schema so that
- Any user can be an owner of n number of brand
- One brand can have n number of owner.
- There could be n number VP in organization.
So i build this model structure..
from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save
from brands.models import Brand
class UserType(models.Model):
EMPLOYEE = 1
BRAND = 2
DEVELOPER = 3
USER_TYPE = (
(EMPLOYEE, "Employee"),
(BRAND, "Brand"),
(DEVELOPER, "Developer"),
)
id = models.PositiveSmallIntegerField(primary_key=True, choices=USER_TYPE)
def __str__(self):
return self.get_id_display()
class CustomUser(AbstractUser):
user_type = models.ManyToManyField(UserType)
verified = models.BooleanField(default=False)
class BrandUser(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
brands = models.ManyToManyField(Brand, through="BrandPosition")
def __str__(self):
return self.user.username
class BrandPosition(models.Model):
OWNER = 1
MANAGER = 2
ASSOCIATE = 3
ROLE = (
(OWNER, "Owner"),
(MANAGER, "Manager"),
(ASSOCIATE, "Associate")
)
user = models.ForeignKey(BrandUser,
on_delete=models.CASCADE)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
position = models.PositiveSmallIntegerField(choices=ROLE)
class Meta:
unique_together = "user", "brand", "position",
def __str__(self):
return "{} as {} in {}".format(self.user.user.username,
self.get_position_display(),
self.brand.name)
class EmployeePosition(models.Model):
CEO = 1
VP = 2
DIRECTOR = 3
MODERATOR = 4
POSITIONS = (
(CEO, "CEO"),
(VP, "VP"),
(DIRECTOR, "Director"),
(MODERATOR, "Moderator"),
)
id = models.PositiveSmallIntegerField(primary_key=True, choices=POSITIONS)
def __str__(self):
return self.get_id_display()
class EmployeeUser(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
position = models.ManyToManyField(EmployeePosition)
class DeveloperUser(models.Model):
pass
Does it match the description or there's something missing??
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/035ed240-6f3d-4d41-a10c-26ab91115cc5%40googlegroups.com.
No comments:
Post a Comment