Malcolm,

Thanks for the advice.  It took me a little bit of fiddling to figure
out that the proper syntax to make your suggestion work but I think I
got it.

form.fields['contact'].choices =
forms.widgets.Select.choices=[(obj.id, obj.__str__()) for obj in
filtered_choices]

I am not sure why you think it is simpler to assign fields to the
instance than to the base_fields since then you have to do it twice
per view, once for the case of the Post request and again for the case
of a blank form.

While I have your attention, I wonder if you can give me your opinion
on this variation:

form.fields['contact'].choices = forms.widgets.Select.choices=[(None,
"- - - - - - - - - - - - - - - -")]+[(obj.id, obj.__str__()) for obj
in filtered_choices]

I did that to replicate the feel of the widget that starts with a
blank choice rather than the first name on the choices list.  It seems
to work.  It is caught by the validator with a nice message saying:
"Please select a valid choice.  That choice is not one of the
available choices." So, it looks right, but it feels like a hack. :)

On Nov 2, 9:25 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Fri, 2007-11-02 at 20:35 +0000, rm wrote:
> > OK, I think I finally got it.  (Thanks to Python's introspection and
> > interactive shell!)  Here is how I got it to work in my view:
>
> > def away_edit(request, away_id):
> >     aw = get_object_or_404(away, id=away_id)
> >     aw_form = forms.form_for_instance(aw)
> >     filtered_choices = Contact.objects.filter(is_staff=True)
> >     aw_form.base_fields['contact'].widget =
> > forms.widgets.Select(choices=[(obj.id, obj.__str__()) for obj in
> > filtered_choices])
>
> Usually ,assigning to base_fields is overdoing things and diving a bit
> too deep.
>
> If you can, I'd suggest keeping things simple and just assigning to
> fields on the *instance* of the form. So, something like this:
>
>         aw_form = forms.form_for_instance(aw)
>         form_instance = aw()
>         form_instance.fields['contact'].choices = ...
>
> The reason for doing it this way is that the 'contact' Field instance
> has a property for "choices", so when you assign to choices, it updates
> both the field's copy and the widget's version in sync. It's also just a
> bit neater -- you only have to worry about "fields" and not
> "base_fields" plus "fields" (you usually can't avoid "fields" if you're
> using newforms).
>
> Regards,
> Malcolm
>
> --
> A clear conscience is usually the sign of a bad 
> memory.http://www.pointy-stick.com/blog/


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