Django: accessing the model instance from within ModelAdmin?

I think you might need to approach this in a slightly different way – by modifying the ModelForm, rather than the admin class. Something like this:

class OrderForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.fields['parent_order'].queryset = Order.objects.filter(
            child_orders__ordernumber__exact=self.instance.pk)

class OrderAdmin(admin.ModelAdmin):
    form = OrderForm

Leave a Comment