Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: accessing the model instance from within ModelAdmin?
    text
    copied!<p>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.</p> <pre><code>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 </code></pre> <p>I've registered an OrderAdmin class for the admin site. For the detail view, I've included <code>parent_order</code> in the <code>fieldsets</code> 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; <code>parent_order</code> is NULL/None), no orders should be displayed. For orders that have been split, this should only display the single parent order.</p> <p>There's a rather new ModelAdmin method available, <code>formfield_for_foreignkey</code>, 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</p> <pre><code>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) </code></pre> <p>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.</p> <p>Of course, we can't hard-code the order number there. We need a reference to the <code>ordernumber</code> field of the order instance whose detail page we're looking at. Like this:</p> <pre><code>kwargs["queryset"] = Order.objects.filter(child_orders__ordernumber__exact=?????) </code></pre> <p>I've found no working way to replace ????? with a reference to the "current" Order instance, and I've dug pretty deep. <code>self</code> inside <code>formfield_for_foreignkey</code> refers to the ModelAdmin instance, and while that does have a <code>model</code> attribute, it's not the order model instance (it's a ModelBase reference; self.model() returns an instance, but its ordernumber is None). </p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload