After considering the severe disadvantage imposed by lacking a
handling case for the 'add' view, I figured it out and wrote it up
here, for the sake of more verbose documentation:

http://mangos.dontexist.net/blog/?p=345

Primarily, the idea is that for inlines, I can grab the hidden field
providing the id of the primary parent field.  Using that id I can
traverse up to the parent, and then back down to filter options
accordingly.  For non-inline 'add' view cases, this is still an issue,
but if you can fill out enough required fields, you can actually do
something like the following:

class MyInlineForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        forms.ModelForm.__init__(self, *args, **kwargs)
        if 'instance' in kwargs:
            instance = kwargs['instance']
        else:
            instance = MyParentModel.objects.get(pk=tuple(i[0] for i
in form.fields['myparentmodel'].widget.choices)[1])
        self.fields['othermodel'].queryset = OtherModel.objects.filter
(parent=instance)

Sorry if the abstraction to generic names is hard to read.  Basically
you get into the 'choices' on the hidden field (which are oddly still
fully assembled, with u'--------' and all) and grab the only id in the
list, using it to look up the proper model.  You could forgo the
actual model lookup, and just filter OtherModel by a lookup to the id
only, but in complex cases like my own (I've only touched the surface
with the Company-Contract-Location scenario) I need the model instance
itself.

Tim

On Nov 5, 4:37 pm, Tim Valenta <tonightslasts...@gmail.com> wrote:
> I don't know what changed, but updating my SVN in the last week
> sometime has fixed the problem that I was having.
>
> The dev trunk and I have a love-hate relationship :)
>
> I would make a note to anybody reading this that if the 'add' view is
> being used, you should kick off the code block with a "if 'instance'
> in kwargs"
>
> Working code:
>
>   # used in a direct subclass of ModelAdmin
>   class ContractForm(forms.ModelForm):
>       def __init__(self, *args, **kwargs):
>           forms.ModelForm.__init__(self, *args, **kwargs)
>           if 'instance' in kwargs:
>               contract = kwargs['instance']
>               self.fields['locations'].queryset =
> Location.objects.filter(company=contract.company)
>
>   # used in a TabularInline of that same ModelAdmin's inlines
>   class EventInlineForm(forms.ModelForm):
>       def __init__(self, *args, **kwargs):
>           forms.ModelForm.__init__(self, *args, **kwargs)
>           if 'instance' in kwargs:
>               contract = kwargs['instance']
>               self.fields['location'].queryset =
> Location.objects.filter(company=contract.company)
>
> Thank you, God.  I've been pulling my hair out over this.
>
> Tim
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to