On Dec 24, 4:24 pm, hotani <hot...@gmail.com> wrote:
> I have close to 600 users split by 20 or so offices. We have a
> helpdesk form (using ModelForm) which displays an initial user list
> based on the office, or no users if an office is not selected.
>
> An AJAX function on the front end dynamically changes users in the
> drop-down based on the office selection. An admin can change the
> office which will display a new set of users to choose from.
>
> Everything is fine till it comes to django validating the form. Since
> the user selection coming in for processing may not have been one of
> the initial choices, I keep getting the dreaded "That choice is not
> one of the available choices" error.
>
> I can hack this up by first sending all users to the field, then
> erasing and resetting the display on the front end but would like a
> more "standard" solution.
>
> Using clean_user(self) does not work - I can throw errors from there
> but don't seem to have a way to tell django "this value is OK,
> dammit!"
>
> ??

You could try dynamically changing the field choices in the form's
__init__ method on post. Something like:

class MyForm(forms.ModelForm):
    def __init__(self, data, *args, **kwargs):
        super(MyForm, self).__init__(data, *args, **kwargs)
        if data:
            office = data['office']
            users = office.user_set.all() # or whatever
            self.fields['users'].choices = [(u.id, u.name) for u in
users]

--
DR.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to