Home > OS >  Should i create one model for multiple apps in a clothing project?
Should i create one model for multiple apps in a clothing project?

Time:12-10

It's my first time creating a project with Django. and it's an e-commerce store for clothing. my confusion now is, most of the items are similar, like women's wears, men's wears, children's wears.Creating different models in different apps means i have to repeat a lot of code because most of the fields will be same and some operations would be more complicated to achieve like global search, etc.

So should i create a single database for all of the items and filter out what i need in different sections like menswear page, women's wear page, etc ?

#For men's model

from django.db import models
from django.urls import reverse

# Create your models here.

CLOTH_CATEGORY_OPTIONS = (
    ("top", "Top"),
    ("bottom", "Bottom"),
    ("complete", "Complete"),
)
CLOTH_GENDER_OPTIONS = (
    ("male", "Male"),
    ("female", "Female"),
    ("unisex", "Unisex"),
)


class Wear(models.Model):
    cloth_name = models.CharField(max_length=50, unique=True)
    cloth_image = models.CharField(max_length=300)
    cloth_category = models.CharField(
        max_length=8, choices=CLOTH_CATEGORY_OPTIONS, default='top')
    cloth_gender = models.CharField(
        max_length=8, choices=CLOTH_GENDER_OPTIONS, default='male')
    cloth_price = models.FloatField()
    cloth_description = models.TextField()
    slug = models.SlugField(default='', editable=False,
                            max_length=200, null=False)

#For women's model

from django.urls import reverse

# Create your models here.

CLOTH_CATEGORY_OPTIONS = (
    ("top", "Top"),
    ("bottom", "Bottom"),
    ("complete", "Complete"),
)
CLOTH_GENDER_OPTIONS = (
    ("male", "Male"),
    ("female", "Female"),
    ("unisex", "Unisex"),
)


class WomensWear(models.Model):
    cloth_name = models.CharField(max_length=50, unique=True)
    cloth_image = models.CharField(max_length=300)
    cloth_category = models.CharField(
        max_length=8, choices=CLOTH_CATEGORY_OPTIONS, default='top')
    cloth_gender = models.CharField(
        max_length=8, choices=CLOTH_GENDER_OPTIONS, default='male')
    cloth_price = models.FloatField()
    cloth_description = models.TextField()
    slug = models.SlugField(default='', editable=False,
                            max_length=200, null=False)

As you can see, this is repetitive, so i want to create a global database so i can import it in each app and filter out for men, women, children etc.

CodePudding user response:

You didn't need to repeat all this code for each gender.. Make one model with a field Gender and specify all the genders. You can filter the clothes in the views with:

Wear.objects.all().filter(Gender=**Male or Women**)

That will filter your products with the gender you give.

CodePudding user response:

Creating 2 models will work for you. A Cloth Model and a Gender Model One cloth can be of a male or female or both so will work with one to many

class Cloth(models.Model):
     gender = models.ForiegnKey(Gender)
     ....

You will be filtering using:

Cloth.objects.filter(gender__type="male")
  • Related