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/cbbf169b6aaa3f39/3f08ba5b59feea3d?lnk=gst&q=form_for_model+foreign+key+filter#3f08ba5b59feea3d

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
-~----------~----~----~----~------~----~------~--~---

Reply via email to