|Admin Inline empty extra - how to access parent form model ID?||mixit||4/28/10 2:48 AM|
Using an Inline in an Admin form:
The main Form model has various text fields, and one Inline, using a
custom little form of its own.
The Inline model has an parentFK pointing to the parent Form model and
has an different FK to a large table, which is displayed as a popup
(or dropdown) menu with ALL its entries - all works perfectly :o)
I want to filter the Inline's FK popup menu (because its too large) by
the ID of the parent model instance (plus various other factors).
Working part of the Implementation:
A "populated" (i.e. already 'saved') inline has an instance, thus I
can access the parentFK from the Inline via 'self.instance.parentFK"
and filter the popup menu by filtering its queryset... again works
The Inline has 'extra: 1' set, to give me one empty extra Inline.
This empty extra however has NO instance before saving (i.e.
self.instance is None), therefore I cannot use the above technique to
get to its parent model, so I cannot filter its popup menu as before :
Horrible hack workaround:
hunt for the popup field name,
grab the request.META['PATH_INFO']
regexp it and pull out the parent model ID.
finally, use the parent model ID thus acquired to filter the popup
Is there no other way to access the parent Form model ID from an
unsaved, "empty" Inline?
I've spent a couple of days wading through the Admin code but can't
find anyway for an empty, "extra" Inline to know anything about the
form model it is embedded in...
Can anyone advise if there is some nifty field, attribute or function
that I am missing?
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
|Re: Admin Inline empty extra - how to access parent form model ID?||Daniel Roseman||4/28/10 5:55 AM|
The inline formset itself (rather than its forms) has an "instance"
attribute which refers to the instance of the parent form.
|Re: Admin Inline empty extra - how to access parent form model ID?||mixit||4/28/10 9:58 AM|
Ah, ok, this isgetting interesting; the formset, eh?
I'm guessing I get to the formset from the parentForm somehow...
Right, I shall start poking around and see if I can find the link.
Many thanksfor the pointer!
|Re: Admin Inline empty extra - how to access parent form model ID?||Prosper Sévère||5/31/12 3:53 AM|
Sorry to bring back a two-years old post, but is there still a way to do this in django 1.4 ?
I'm exploring the BaseInlineFormset object but there's no "instance" attribute anymore
|Re: Admin Inline empty extra - how to access parent form model ID?||Prosper Sévère||5/31/12 7:25 AM|
I found a noce solution, it only needs this tiny patch already proposed on the tracker :
There's no reason this patch could not be merged in the trunk, it's self-contained
With this, I can override get_inline_instances in my ModelAdmin :
def get_inline_instances(self, request, obj):
inline_instances = 
for inline_class in self.inlines:
if inline_class.model == get_model('coop_local', 'Exchange'):
inline = inline_class(self.model, self.admin_site, obj=obj)
inline = inline_class(self.model, self.admin_site)
The inline gets the obj parameter , puts it in a local attribute and deletes it to not bother the Inline superclass
def __init__(self, *args, **kwargs):
self.parent_object = kwargs['obj']
del kwargs['obj'] # superclass will choke on this
super(ExchangeInline, self).__init__(*args, **kwargs)
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'location':
kwargs['queryset'] = self.parent_object.locations()
return super(ExchangeInline, self).formfield_for_dbfield(db_field, **kwargs)
An finally in formfield_for_dbfield I can use parent_object to correctly filter my queryset, and this filtering works in any line of the formset : those which are bound to existing objects and the blank (extra ) ones !