Re: Admin Inline empty extra - how to access parent form model ID?
On Apr 28, 10:48 am, mixit <s...@blueyonder.co.uk> wrote:
> 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
> perfectly :o)
>
> Problem:
> 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:
> use formfield_for_foreignkey(),
> 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
> menu queryset.
>
> Question:
> 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?
> regards
> si
>
The inline formset itself (rather than its forms) has an "instance"
attribute which refers to the instance of the parent form.
--
DR.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home