What you really need is to use the formfield_callback argument to form_for_instance. This allows you to override the default fields that are returned for a given field in the model [1]. There is an open bug, [2], that will allow for an easier way of overriding the queryset that is ultimately rendered as the options. Until then you will just need to subclass ModelChoiceField and re-implement the choices property to use a custom queryset that does the filtering you are describing.
[1] http://www.djangoproject.com/documentation/newforms/#overriding-the-default-field-types [2] http://code.djangoproject.com/ticket/4787 On Nov 1, 3:15 pm, rm <[EMAIL PROTECTED]> wrote: > I know this is a painful subject. I have spent a couple of days > trying to figure this out, and I am almost there. (I think. :) > > Here is the model (simplified to bare bones): > > class away(models.Model): > contact = models.ForeignKey(Contact, verbose_name="Name of person > away", limit_choices_to= {'is_staff':True}, db_column='contact') > > As you can see Contact is another model being referenced here which > has a field named "is_staff" that I want to use to filter out some > contacts so that they do not display as options in my form to add or > edit away instances. Unfortunately, the filter seems to only affect > the way the field is displayed in the admin forms, not on regular > forms. > > This is how the start of my view to edit away instances looks like: > > def away_edit(request, away_id): > aw = get_object_or_404(away, id=away_id) > aw_form = forms.form_for_instance(aw) > > When displaying that form the 'contact' field displays as a dropdown > list, but it includes the choices I wanted filtered out. So, after a > lot of searching, I found this post which seems to be addressing the > same issue: > > http://groups.google.com/group/django-users/browse_thread/thread/cbbf... > > However, I can't make it work. This is how I have translated the > advice to my view: > > def away_edit(request, away_id): > aw = get_object_or_404(away, id=away_id) > aw_form = forms.form_for_instance(aw) > aw_form.base_fields['contact']=forms.ChoiceField(choices=[(obj.id, > obj.name) for obj in Contact.objects.filter(is_staff=True)]) > > When I do that, the form displays correctly and it actually does > filter out the choices as I want. But, when I submit the form I get > an error telling me that "away_away.contact may not be NULL". > > Can someone please explain to me why it gives me that error? --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---