Filter dropdown choices based on another fields value: [Django, django-smart-selects]

30 views Asked by At

I want to show only relevant option in first_model_field dropdown based on values of product and product_state. When admin changes values of one of these fields in SecondModel, it should get only first_model_field choices that have same state and product. How can I get filtered FirstModel based on these two values without writing async JavaScript ? It should also change when one of these values changes in dropdown.

class FirstModel(models.Model):
    product = models.ForeignKey("Product", on_delete=models.CASCADE)
    product_state = models.CharField(default=PRODUCT_STATE_START, choices=PRODUCT_CHOICES)

class SecondModel(models.Model):
    product = models.ForeignKey("Product", on_delete=models.CASCADE)
    product_state = models.CharField(default=PRODUCT_STATE_START, choices=PRODUCT_CHOICES)
    first_model_field = ChainedForeignKey(
        FirstModel,
        on_delete=models.CASCADE,
        chained_field="product",
        chained_model_field="product",
    )

ps: Currently it only filters based on product field

0

There are 0 answers