Here’s the logic for the models:
- Category. There are several categories; each category can contain several products.
- Product. There are several products; each product can only have one category.
Is it possible to specify what kind of category each product is within the model file itself? For example: can I set the model so that a shirt can only be clothing and nothing else?
Here’s what I have so far (it doesn’t validate):
class Category(models.Model):
CATEGORY_CHOICES = (
('CLOTHING', 'Clothing'),
('FURNITURE', 'Furniture'),
)
category = models.CharField(choices=CATEGORY_CHOICES)
class Shirt(Product):
category = models.ForeignKey(Category, default=CATEGORY_CHOICES.CLOTHING)
class Table(Product):
category = models.ForeignKey(Category, default=CATEGORY_CHOICES.FURNITURE)
I’m new at this. Thanks for the help!
You can validate your model on save with any arbitrary rules. So, write a validation rule that checks that all shirts are saved in the category clothing.
For user input, create a form that only provides choices corresponding to the product.
Good luck!