> I don't think there's any way of accessing the request.user from the
> Form class (is there?). If I could pass in the choices when I'm
> instantiating the form from views.py my problem would be solved. Is
> this possible?

Cathy this is absolutely possible.  Here is an example from one of my
projects...

First here is the form:

---------- cut here ----------
class AddEditContactPhoneForm(forms.Form):
    employee_id = forms.ChoiceField(label='Employee', required=False)
    phone_number = PhoneNumberField(label='Phone Number')
    extension = forms.CharField(max_length=8,required=False)
    phone_type_id = forms.ChoiceField(label='Phone Type',
        choices=[(c.id,c.description) for c in
PhoneNumberType.objects.all()] )
    def __init__(self, data=None, auto_id='id_%s', prefix=None,
employee_list={}):
        super( AddEditContactPhoneForm,
self ).__init__( data,auto_id,prefix )
        self.fields['employee_id'] =
forms.ChoiceField(label='Employee', required=False,
            choices = [('','Select')] + [(c.id,"%s, %s (%s)" %
(c.last_name,c.first_name,c.title)) for c in employee_list] )
---------- cut here ----------

And here is a view that uses it:

---------- cut here ----------
@login_required
def customer_phone_numbers_detail(request,customer_id,object_id):
    """
    Editing form for a phone number
    customer_id is the Customer.id
    object_id is the ContactPhone.id of the phone number to edit or 0
for new phone number
    """
    customer = Customer.objects.get(id=int(customer_id))
    employees = Employee.objects.filter(customer=customer,active=True)
    object_id = int(object_id)
    error_message = None
    if request.method == "POST":
        edit_form =
AddEditContactPhoneForm(request.POST,employee_list=employees)
        if edit_form.is_valid():
            obj = ContactPhone(**edit_form.clean_data)
            if object_id > 0 : obj.id = object_id
            obj.customer = customer
            obj.save()
            object_id=obj.id
    elif object_id > 0:
        obj = ContactPhone.objects.get(id=object_id)
        edit_form =
AddEditContactPhoneForm(obj.__dict__,employee_list=employees)
    else:
        edit_form = AddEditContactPhoneForm(employee_list=employees)
    return render_to_response("kindledb/
customer_phone_numbers_detail.html",
        {"edit_form": edit_form, 'object_id': object_id,
            'error_message': error_message, 'customer' : customer})
---------- cut here ----------

--gordy


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