Answer the question
In order to leave comments, you need to log in
How to dynamically filter a field in a form?
There are three
models
class Partner(models.Model):
partner_name = models.CharField(max_length=30, verbose_name='Имя контрагента')
class Contract(models.Model):
contract_name = models.CharField(max_length=30, verbose_name='Название')
partner = models.ForeignKey(Partner, on_delete=models.CASCADE, verbose_name='Партнер')
class Work(models.Model):
work_name = models.CharField(max_length=30, verbose_name='Наименование')
contract = models.ForeignKey(Contract, on_delete=models.CASCADE, verbose_name='Договор')
Answer the question
In order to leave comments, you need to log in
This functionality can be implemented by reloading the form passing the selected values via GET parameters, but it's much easier and more convenient to use Select2 / autocomplete_fields. In addition to filtering, this solves two more problems: loading long lists and searching for elements in such lists.
Without js, filtering the values of dependent fields can only be implemented using a multi-step form: a form with a single field Partner -> Submit -> a form with all other fields.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question