>         forms.ModelForm.__init__(self, *args, **kwargs)
>         location = kwargs.get('instance', None)
>         if location:
>             self.fields['contract'].queryset = Contract.objects.filter
> (company=location.company)

It seems that editing the values in self.fields yields rendering
errors:

    Caught an exception while rendering: 'QuerySet' object has no
attribute 'label'

It appears that the values are not vanilla QuerySets... I've been
browsing the code again, and I really don't know what the values are.
It's fairly convoluted with that __metaclass__ business in there.  Do
we know what kind of datatype is in self.fields?  When the template
iterates the Form object, it's wrapping it as a BoundField, but
clearly something's messing up during that iteration.  I'm afraid I
don't know much about how the mechanics of this area work in the
Python code.

Any more pointers would be great.  Other than this speedbump, I think
this will do nicely.

Tim

On Oct 26, 8:53 am, Tim Valenta <tonightslasts...@gmail.com> wrote:
> Many many thanks for the response.
>
> I had tried that approach, but had no idea what was coming through in
> kwargs.  I feel like 'kwargs' on most class objects needs more
> thorough documentation for the general users who refer primarily to
> the on-site docs.  Even digging through some code, I simply had no
> idea.
>
> This should provide a working fix for the sort of filtering I need to
> do.  I hope that maybe I or some other person can provide some code to
> help simplify this amazingly common dilemma.  Any models that group in
> logical relationships of 3 seem to always have this problem, and I'd
> love to have a simpler fix to write off in the docs.
>
> Tim
>
> On Oct 25, 8:28 pm, Matt Schinckel <matt.schinc...@gmail.com> wrote:
>
>
>
> > On Oct 24, 5:14 am, Tim Valenta <tonightslasts...@gmail.com> wrote:
>
> > > I've been searching for a while on how to intercept querysets in
> > > forms, to apply a custom filter to the choices on both foreignkey and
> > > m2m widgets.  I'm surprised at how there are so many similar questions
> > > out there, dating back to 2006.
>
> > [snip]
>
> > > The only solution I've seen has dealt with filtering by User foreign
> > > key (being that User is available in the request object in views), and
> > > that's primarily for non-Admin views.
>
> > [snip]
>
> > > I've been looking at the code for the above noted API method,
> > > formfield_for_foreignkey, and I really can't see a way to get
> > > references to an instance of the object being edited.  I would need
> > > such a reference to successfully override that method and filter my
> > > queryset on this relationship.
>
> > I too spent some time looking at formfield_for_foreignkey, and had no
> > luck.
>
> > You can subclass ModelAdmin, and then limit the objects in the field's
> > queryset.
>
> > ** admin.py **
>
> > class LocationAdminForm(forms.ModelForm):
> >     def __init__(self, *args, **kwargs):
> >         forms.ModelForm.__init__(self, *args, **kwargs)
> >         location = kwargs.get('instance', None)
> >         if location:
> >             self.fields['contract'].queryset = Contract.objects.filter
> > (company=location.company)
>
> > class LocationAdmin(admin.ModelAdmin):
> >     model = Location
> >     form = LocationAdminForm
>
> > I also had to do something similar with Inlines, when I did the same
> > type of thing.  This is not my exact code, but it is very close, and
> > suited toward your use case.
>
> > Matt.
--~--~---------~--~----~------------~-------~--~----~
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