If i’m creating i.e. an item in /admin/product/ i get some 2 fields that are described in product/models.py class Item: category and name;
I would like a 3rd field appear dynamically while creating an item depending on chosen category(a or b):
CATEGORIES = (
('a', 'Apple'),
('b', 'Banana'),
)
class Item(models.Model):
category = models.ForeignKey(choices=CATEGORIES)
name = models.CharField(max_length=255)
"""
category.a:
color = models.CharField(max_length=21)
category.b:
count = models.CharField(max_length=21)
"""
The field name will be as for a so for b categories and (color or count) field will appear only for (a or b) categories respectively;
Any help is greatly appreciated. Thanks.
P.S.
Sorry for someway incorrect title;
I think what you’re trying to do is have models that change based on the category.
The way I would do this is to define an abstract model (probably called fruit, in this case) with all the common information, and then inherit from it in a model called bananas and a model called apples.
To add a new banana/apple, then, I would use either the appropriate admin form or add logic to the appropriate view that determines which sort of object to add.
https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes
Edit: Unfortunately I don’t know how the admin interface deals with abstract classes, and I don’t have a test install to hand. As for the logic, in the view I would expect the logic to be something like: