1
\$\begingroup\$

I am developing a project with various tables so I can have better understanding of model relation by communicating with the experts. Can anyone look at my code and see if I have designed my models based on the following conditions or not (only the relation in the model)?

Users

There are 3 types of users:

  1. Manufacturer
  2. Agency
  3. Agents

Manufacturer can create Agency and Agents. Agency can create Agents. Agents can create their own account and Agency or Manufacturers can search and assign that agent under their ownership.

Manufacturer

Name

Address:

City

Street

Country

Contact Number (At least 3 numbers can be added)

Email

VAT/PAN Number

Agency ============= Name

Address:

City

Street

Country

Contact Number (At least 3 numbers can be added)

Email

VAT/PAN Number

Agent

DOB

Sex

Address:

City

Street

Country

Mobile Number (At least 2 numbers can be added)

[All the below fields should contain user information regarding Who Created it?. Like, Which agent added that shop, which agent took that order?)

Products

Product Code

Name

Category (User should be able to select single category)

Packaging

Package Dimension (L x B x H)

Package Weight

Photo

Selling Price

Note (For discount note purpose. Buy 1 cartoon and get 2 pcs. free)

Shop

Name

Address:

City

Street

Country

Coordinate

Contact (Multiple)

Photo

VAT/PAN Number

Owner Name

Order

Shop (user should be able to select shop from listing)

Product (user should be able to select product from listing)

Quantity

Unit

DateTime (order date time)

Due Date

Here is my code to fulfill the following condition:

accounts/models.py

class Role(models.Model):

    ROLE_CHOICES = (
        ('agent', 'Agent'),
        ('agency', 'Agency'),
        ('manufacturer', 'Manufacturer'),
    )
    role = models.CharField(max_length=15, choices=ROLE_CHOICES)

    def __str__(self):
        return self.role


class User(AbstractUser):

    role = models.ForeignKey(
        Role,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )

    def __str__(self):
        return self.username

    def save(self, *args, **kwargs):
        if not self.pk:  # the instance is created
            self.role, created = Role.objects.get_or_create(role="agent")
        return super().save(*args, **kwargs)




agency/models.py

class Agency(models.Model):

    owner = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(
        max_length=200,
        blank=False,
        null=False)
    city = models.CharField(max_length=150, blank=False, null=False)
    street = models.CharField(max_length=150, blank=True, null=True)
    country = models.CharField(max_length=150, blank=True, null=True)
    mobile_number = PhoneNumberField()
    email = models.EmailField(blank=False, null=False)
    vat_number = models.CharField(max_length=40, blank=False, null=False)

agents/models.py

class Agent(models.Model):

    SEX_CHOICE = (
        ('male', 'Male'),
        ('female', 'Female'),
    )
    owner = models.OneToOneField(User, on_delete=models.CASCADE)
    agencies = models.ForeignKey(
        Agency,
        related_name="agents",
        on_delete=models.CASCADE)
    manufacturers = models.ForeignKey(
        Manufacturer,
        related_name="agents_manufacturer",
        on_delete=models.CASCADE,
        blank=True,
        null=True)
    date_of_birth = models.DateField(blank=True, null=True)
    sex = models.CharField(max_length=6, choices=SEX_CHOICE)
    city = models.CharField(max_length=150, blank=False, null=False)
    street = models.CharField(max_length=150, blank=True, null=True)
    country = models.CharField(max_length=150, blank=True, null=True)
    mobile_number = PhoneNumberField()



class Manufacturer(models.Model):

    owner = models.OneToOneField(
        User, on_delete=models.CASCADE, related_name="manufacturer")
    agency = models.ForeignKey(
        Agency,
        blank=True,
        null=True,
        related_name="agency_manufacturer",
        on_delete=models.CASCADE)
    name = models.CharField(
        max_length=200,
        blank=False,
        null=False)
    city = models.CharField(max_length=150, blank=False, null=False)
    street = models.CharField(max_length=150, blank=True, null=True)
    country = models.CharField(max_length=150, blank=True, null=True)



shops/models.py

class Shop(models.Model):

    agent = models.ForeignKey(Agent, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=150, blank=False, null=False)
    city = models.CharField(max_length=150, blank=False, null=False)
    street = models.CharField(max_length=150, blank=True, null=True)
    country = models.CharField(max_length=150, blank=True, null=True)
    mobile_number = PhoneNumberField()
    vat_number = models.CharField(max_length=40, blank=False, null=False)

    def __str__(self):
        return self.name


class Category(models.Model):

    name = models.CharField(max_length=150, db_index=True)
    slug = models.SlugField(max_length=150, db_index=True)
    description = models.TextField()
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.name


class Product(models.Model):

    product_code = models.CharField(max_length=50, blank=False, null=False)
    agencies = models.ForeignKey(
        Agency,
        on_delete=models.CASCADE,
        related_name="product_agencies",
        null=True)
    manufacturer = models.ForeignKey(
        Manufacturer,
        related_name="product_manufacturers",
        on_delete=models.CASCADE,
        null=True)
    name = models.CharField(max_length=120, db_index=True)
    category = models.ForeignKey(
        Category,
        related_name="product_category",
        null=True,
        on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    min_price = models.DecimalField(max_digits=10, decimal_places=2)
    packaging = models.CharField(max_length=120)
    package_dimension = models.CharField(max_length=120, blank=True, null=True)
    package_weight = models.PositiveIntegerField(default=1)


orders/models.py

class Order(models.Model):

    product_code = models.CharField(max_length=50, blank=False, null=False)
    agents = models.ForeignKey(
        Agent,
        on_delete=models.CASCADE,
        related_name="order_agents")
    shops = models.ForeignKey(
        Shop,
        related_name="order_manufacturers",
        on_delete=models.CASCADE)
    product = models.ForeignKey(
        Product,
        related_name="order_product",
        on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    unit = models.CharField(max_length=50)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.