Django limit_choices_to for multiple fields with the condition "or"

I am trying to limit the selection for a field by checking the values โ€‹โ€‹of two columns: "share_holder" and "distributor". If any of them is true, then I want this choice.

With the version below, I only got a choice that satisfies both conditions ("share_holder": True AND 'distributor': True).

limit_choices_to={'share_holder': True, 'distributor': True} 

However, I need options for ("share_holder": True OR 'distributor': True).

+6
source share
1 answer

You can use Q objects to achieve this.

 from django.db.models import Q limit_choices_to=Q(share_holder=True) | Q(distributor=True) 

White papers at ForeignKey.limit_choices_to

+11
source

Source: https://habr.com/ru/post/987012/


All Articles