Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by GalacticScout260

How can I dynamically update ModelChoiceField querysets based on user input in Django?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

Hi all,

I need help dynamically updating the queryset of one ModelChoiceField based on the value selected in another ModelChoiceField (and possibly other input fields). For example, I have two ModelChoiceFields defined as follows:

PYTHON
assigned1Id = forms.ModelChoiceField(queryset=models.User.objects.all().filter(status=True), empty_label="Name") assigned2Id = forms.ModelChoiceField(queryset=models.Properties.objects.all().filter(status=True), empty_label="Properties")

I want the following behavior:

  1. If no User is selected, the second ModelChoiceField should display all Properties.
  2. If a User is selected in the first ModelChoiceField, the second ModelChoiceField should update its queryset in real time to apply an additional filter.

This dynamic updating should also be applicable to other input fields (such as a CharField) where entering a value would filter the properties list in the ModelChoiceField.

Is this possible? Any guidance or examples of how to achieve this would be greatly appreciated. Thank you!

0

Comments 0

Answers 0

Views 59

0 Answers

Discussion

No comments yet.