S
S
shibanovan2019-06-25 10:29:05
Django
shibanovan, 2019-06-25 10:29:05

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='Имя контрагента')

Treaty
class Contract(models.Model):
    contract_name = models.CharField(max_length=30, verbose_name='Название')
    partner = models.ForeignKey(Partner, on_delete=models.CASCADE, verbose_name='Партнер')

Work
class Work(models.Model):
    work_name = models.CharField(max_length=30, verbose_name='Наименование')
    contract = models.ForeignKey(Contract, on_delete=models.CASCADE, verbose_name='Договор')

I want to display only his contracts when creating a work, when choosing a partner_name in the form. Is there any way out other than using JS-Frontend ? Have you solved similar problems, how did you implement them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neatsoft, 2019-06-25
@shibanovan

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 question

Ask a Question

731 491 924 answers to any question