0

I have one model:

class Product(models.Model):
    slug = models.SlugField(unique=True)
    image = models.ImageField(upload_to='media')
    title = models.CharField(max_length=150)
    description = models.TextField()
    short_description = models.CharField(max_length=300)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    discount_price = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True) 
    stock_quantity = models.PositiveIntegerField(default=10)
    in_stock = models.BooleanField(default=True)
    on_sale = models.BooleanField(default=False)
    date_added = models.DateTimeField(auto_now_add=True, blank=True)
    main_category = models.CharField(default='FW', choices=CATEGORY_CHOICES, max_length=2)
    additional_category = models.CharField(choices=CATEGORY_CHOICES, max_length=2, blank=True, null=True)
    brand = models.CharField(choices=CATEGORY_CHOICES, max_length=2, blank=True, null=True)
    size = models.CharField(max_length=2, choices=CATEGORY_CHOICES, default='M', blank=True, null=True)


    def __str__(self):
        return self.title

Within this model will be various types of products for example, clothes and books. These will be defined by the main_category and additional_category fields within the model. If the product being entered into the database is a piece of clothing I will enter values for size and brand. For books, these will obviously be blank.

However, if the product is a piece of clothing, I have clothing for women, clothing for men and clothing for children. I will be able to define these by selecting 'for women' in the main_category field.

In my templates, I would like to dynamically display a page that shows all clothes, one that displays 'clothes for women', another page that shows 'clothes for men' etc etc. And then a page that displays all books, books for teens, books for adults etc, based on the main_category and additional_category.

If i enter a product, and the main category is 'for women' and the additional category is 'plus size', I would like a template to dynamically display all products that have either 'for women' selected in main_category OR additional_category. And a template that displays all plus size clothing, where main_category OR additional_category is 'plus size'.

My trouble comes when I am writing the views. Do I need to provide a view for women only clothes, plus size only clothes, children's clothes, books, books for adults, books for teens etc. Or can I write just ONE view, that outputs the correct data to multiple templates. Basically, do I need one view per template?

I have read this:

https://docs.djangoproject.com/en/3.0/topics/class-based-views/generic-display/

and have taken a look at get_context_data. Can I define multiple get_context_data functions to return different filtered information for different templates, and if so how do I differentiate between them?

I am current learning, and building a dummy online store with Django in order to learn as much as possible, but finding this particularly complex?

Thanks

CThomas
  • 199
  • 1
  • 10

1 Answers1

0

A cleaner way to do that is to create an abstract model called Product and use it as a base class for different product types, like clothes and books.

Being Product an abstract class means that Django's ORM won't create a Product table on database. Instead, it will create tables for each subclass including the common fields you define on Product class. Check the code below for better understanding:

# models.py
from django.db import models

class Product(models.Model):
    image = models.ImageField(upload_to='media')
    title = models.CharField(max_length=150)
    # ... (other common fields)

    class Meta:
        abstract = True

class Clothes(Product):
    size = models.CharField(max_length=2, choices=CATEGORY_CHOICES, default='M', blank=True, null=True)
    # ... (other specific fields)

From django docs.

oz19
  • 1,616
  • 1
  • 17
  • 22