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