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.

I came across the documentation for
ModelAdmin.formfield_for_foreignkey (http://docs.djangoproject.com/en/
dev/ref/contrib/admin/
#django.contrib.admin.ModelAdmin.formfield_for_foreignkey), and
thought I had a perfect solution, but the method doesn't seem to
entirely fit the hole I'm trying to fill.

Consider these models:

# models.py
class Company(models.Model):
    # ...
class Contract(models.Model):
    company = models.ForeignKey(Company)
    locations = models.ManyToManyField('Location')
class Location(models.Model):
    company = models.ForeignKey(Company)

So, Company is at the top of the hierarchy, with both Contract and
Location holding foreign keys to Company, and then Contract and
Location share an m2m relationship.

The admin presents this data so that the m2m widget on Contract lets
you pick from any Location in the database.  Arguably, there should be
an option for the implied common relationship to take effect, of both
Contract and Location to Company.

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.




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.

Any thoughts?  There are dozens of unanswered questions just like this
all over popular sites on the internet.  We need a solution.  The
feature list for Django 1.2 does not seem to reference anything on
this matter!

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