How to pull up Django model data based on ALREADY filled out form fields for other ForeignKeys

I have a form in which there are several fields with foreign keys. For example, the table "Entrance" (3 entries -> 1st entrance, 2nd entrance, 3rd entrance), "Apartment" (30 entries, 10 for each entrance (No.1-10, No. 11-20 and No. 21-30), "Owner" (also 30 entries of surnames). In turn, the "Owner" has an external key to the apartment, and the "Apartment" has an external key to the entrance. here's the question, if I selected entrance No. 2 in the form, then in the other two fields (Apartment and Owner) I need to display ONLY related records (that is, apartments from 11 to 20 rooms, and the owners who live in these apartments) how to implement it???

here is the code in forms.py(naturally, it gives out an error, I just pointed out where, logically, I wanted to get involved.) ` id_entr = ModelChoiceField(queryset=Bs_entrance.objects.all(), widget=Select( attrs={'class': 'form-select', 'placeholder': 'выберите подъезд'}))

id_apart = ModelChoiceField(queryset=Bs_entrance.objects.filter(id_entrance=**id_entr**),
                                   widget=Select(
                                       attrs={'class': 'form-select', 'placeholder': 'выберите квартиру'}))
id_owner = ModelChoiceField(queryset=Bs_apartment.objects.filter(id_apartment=**id_apart**),
                                   widget=Select(
                                       attrs={'class': 'form-select', 'placeholder': 'выберите собственника'}))

`

enter image description here

Here is an example of the form (the example is not on apartments)There are all kinds of fault groups, although there should have been only two, since the ballast was selected

Вернуться на верх