[Answered ]-Django admin form field – display objecy-dependent query results in a select box

2👍

Found the solution:

class MyAdminModelForm(ModelForm):
    class Meta(object):
        model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyAdminModelForm, self).__init__(*args, **kwargs)

        if not self.instance.pk:
            return # don't fetch related choices for new objects

        current_event = self.instance.event
        self.fields['route'] = ChoiceField(choices=OtherModel.objects.filter(event=current_event).values_list('id', 'title'))


@admin.register(MyModel)
class MyModelAdmin(ModelAdmin):
    form = MyAdminModelForm
👤Dor

Leave a comment