Skip to content Skip to sidebar Skip to footer

How To Get Instance Of Entity In Limit_choices_to (Django)?

For instance: class Foo(models.Model): bar = models.OneToOneField( 'app.Bar', limit_choices_to=Q(type=1) & Q(foo=None) | Q(foo=instance) ) class Bar(m

Solution 1:

If you pass a callable to limit_choices_to, that callable has no reference to the current instance. As such, you can't filter based on the current instance either.

There are several other ways to achieve what you want, such as overriding formfield_for_foreignkey() as you mentioned, or overriding the formfield's queryset in the form's __init__() method. limit_choices_to just isn't one of them.


Post a Comment for "How To Get Instance Of Entity In Limit_choices_to (Django)?"