How to get instance of entity in limit_choices_to (Django)?

前端 未结 1 1969
Happy的楠姐
Happy的楠姐 2020-12-21 10:47

For instance:

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


        
1条回答
  •  一生所求
    2020-12-21 11:36

    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.

    0 讨论(0)
提交回复
热议问题