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(models.Model):
TYPE_CHOICE = (
(0, 'hello'),
(1, 'world')
)
type = models.SmallIntegerField(
choices=TYPE_CHOICE,
default=0
)
I wanna show in Django admin only these Bars that have type = 1
, that haven't relations with Foo's, and show linked Bar of edited entity (if it is).
Of course, we can do it via overriding formfield_for_foreignkey
method of admin.ModelAdmin
, but we want do this via limit_choices_to
.
How to get instance of edited entity?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…