47

I've got a model for Orders in a webshop application, with an auto-incrementing primary key and a foreign key to itself, since orders can be split into multiple orders, but the relationship to the original order must be maintained.

class Order(models.Model):
    ordernumber = models.AutoField(primary_key=True)
    parent_order = models.ForeignKey('self', null=True, blank=True, related_name='child_orders')
    # .. other fields not relevant here

I've registered an OrderAdmin class for the admin site. For the detail view, I've included parent_order in the fieldsets attribute. Of course, by default this lists all the orders in a select box, but this is not the desired behaviour. Instead, for orders that don't have a parent order (i.e. have not been split from another order; parent_order is NULL/None), no orders should be displayed. For orders that have been split, this should only display the single parent order.

There's a rather new ModelAdmin method available, formfield_for_foreignkey, that seems perfect for this, since the queryset can be filtered inside it. Imagine we're looking at the detail view of order #11234, which has been split from order #11208. The code is below

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'parent_order':
        # kwargs["queryset"] = Order.objects.filter(child_orders__ordernumber__exact=11234)
        return db_field.formfield(**kwargs)
    return super(OrderAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

The commented row works when run in a Python shell, returning a single-item queryset containing order #11208 for #11234 and all other orders that may have been split from it.

Of course, we can't hard-code the order number there. We need a reference to the ordernumber field of the order instance whose detail page we're looking at. Like this:

kwargs["queryset"] = Order.objects.filter(child_orders__ordernumber__exact=?????)

I've found no working way to replace ????? with a reference to the "current" Order instance, and I've dug pretty deep. self inside formfield_for_foreignkey refers to the ModelAdmin instance, and while that does have a model attribute, it's not the order model instance (it's a ModelBase reference; self.model() returns an instance, but its ordernumber is None).

One solution might be to pull the order number from request.path (/admin/orders/order/11234/), but that is really ugly. I really wish there is a better way.

3 Answers 3

62

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
4
  • 1
    It works! Thank you so much! I'm completely new to all this ModelForm/ModelAdmin business, and was looking in the wrong place.
    – JK Laiho
    Commented Jun 4, 2009 at 9:12
  • I realize this post is old, but this also worked as a decent workaround for me for for get_readonly_fields on an InlineModelAdmin since the obj parameter passed to it is currently the parent object, not the object of the inline instance. For all intents and purposes, this made my object readonly by allowing me to only return a single object into my foreignkey.
    – DM Graves
    Commented Jun 29, 2012 at 5:29
  • Make sure you call super.__init__ first. That sets self.instance. Commented Apr 16, 2013 at 21:02
  • It will not work if your admin model have autocomplete_fields atribute in admin model Commented Feb 22, 2021 at 9:29
10

I've modeled my inline class this way. It's a bit ugly on how it gets the parent form id to filter inline data, but it works. It filters units by company from the parent form.

The original concept is explained here http://www.stereoplex.com/blog/filtering-dropdown-lists-in-the-django-admin

class CompanyOccupationInline(admin.TabularInline):

    model = Occupation
    # max_num = 1
    extra = 0
    can_delete = False
    formset = RequiredInlineFormSet

    def formfield_for_dbfield(self, field, **kwargs):

        if field.name == 'unit':
            parent_company = self.get_object(kwargs['request'], Company)
            units = Unit.objects.filter(company=parent_company)
            return forms.ModelChoiceField(queryset=units)
        return super(CompanyOccupationInline, self).formfield_for_dbfield(field, **kwargs)

    def get_object(self, request, model):
        object_id = resolve(request.path).args[0]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)
4
  • 8
    A slightly cleaner way to approach this would be to take advantage of the URL resolver: object_id = resolve(request.path).args[0]
    – philipk
    Commented Jan 2, 2014 at 21:28
  • Thank you @philipk. Commented Jul 13, 2018 at 14:16
  • @DanielH. the link was removed by the owner. I will edit the answer. Thanks. Commented Jul 13, 2018 at 14:16
  • In Django 2.2 and 3.1 (source), you can simply use request.resolver_match.kwargs.get('object_id').
    – djvg
    Commented Oct 6, 2020 at 12:16
3

The above answer from Erwin Julius worked for me, except I found that the name "get_object" conflicts with a Django function so name the function "my_get_object".

class CompanyOccupationInline(admin.TabularInline):

    model = Occupation
    # max_num = 1
    extra = 0
    can_delete = False
    formset = RequiredInlineFormSet

    def formfield_for_dbfield(self, field, **kwargs):

        if field.name == 'unit':
            parent_company = self.my_get_object(kwargs['request'], Company)
            units = Unit.objects.filter(company=parent_company)
            return forms.ModelChoiceField(queryset=units)
        return super(CompanyOccupationInline, self).formfield_for_dbfield(field, **kwargs)

    def my_get_object(self, request, model):
        object_id = request.META['PATH_INFO'].strip('/').split('/')[-1]
        try:
            object_id = int(object_id)
        except ValueError:
            return None
        return model.objects.get(pk=object_id)

It told me not to "respond" to others' answers, but I'm not allowed to "reply" yet, and I have been looking for this for a while so hope this will be helpful to others. I am also not allowed to upvote yet or I totally would!

1
  • While it would be better to leave this information as a comment, I understand you don't have enough reputation for that -- knowing get_object causes a collision is valuable though. Commented Oct 29, 2013 at 21:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.