'Filtering in Django which depends on first field
Suppose I have 3 fields of a Car Shop. those are Condition, Brand, Model .
class Car(models.Model):
ConditionType=(('1','New'),('2','Used'),('3','Reconditon'))
BrandType=(
('Toyota','Toyota'),('Honda','Honda'),('Mitsubishi','Mitsubishi'),
('Nissan','Nissan'),('Hyindai','Hyindai')
)
now model will depends on Brand user select . if user select Toyota as brand then >available model for toyota are (Axio,Premio ,Allion etc) ,for Honda carmodels are (civic >,vezel,Grace)
Brand=models.CharField(max_length=120,choices=BrandType,default=None)
Condition=models.CharField(max_length=120,choices=ConditionType,default='New')
CarModel=models.CharField(max_length=120,choices=ModelType,default=None)
Now how can I make carmodel dependent on Brand . Suppose if user choose Brand - Toyota >then user can only choose Carmodel of Toyota if user Choose Honda than he can choose >CarModel of Honda . which means how can I make my model selection dependent on Brand ? if no brand is selected than user won't able to choose CarModel .**
Solution 1:[1]
If i understand right, you need 3 choice field So you have to create a model Contain your ConditionTypeCHOICES and BrandTypeCHOICES and one forigionkey field for your car model (later create choices)
Note :
how to create choice field model
https://stackoverflow.com/a/32657683/15864993
after that you need to create another model for your car model forigion field (contain BrandTypeCHOICES field and character field )
now you can migrate and by accessing to admin panel create your car models and the the other one
if you like to create a form ,you can create a modelform and filter your choices in form For your car model to only Show the carmodels to the brand
Note: here is a sample of how to filter your carmodel choices by brand https://stackoverflow.com/a/21802408/15864993
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hamidipour_AM |
