Reputation: 2884
Here's the logic for the models:
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!
Upvotes: 0
Views: 157
Reputation: 32424
I suggest that you invest some time into adopting recently added model validation, while it's not automatic as forms validation (you'll have to call clean*
methods yourself, probably inside save
), you gonna get DRY validation that could be used on Model and Form level.
Upvotes: 1
Reputation: 10177
You can use callables to give instance as a default value
Something like this (untested code):
class Shirt(Product):
def getClothingInstance():
return Category.objects.get(category=Category.CATEGORY_CHOISES['CLOTHING'])
category = models.ForeignKey(Category, default=getClothingInstance)
Upvotes: 0
Reputation: 7741
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!
Upvotes: 1