Modelform and Ajax (Select a valid choice..... Error)

391 views
Skip to first unread message

siddharth ghumre

unread,
Dec 11, 2012, 6:39:48 AM12/11/12
to django...@googlegroups.com
Hi,

I am facing problem in handling modelform and ajax together.
The situation is:-
I am using modelform to get display the form as ul in the template.
I have 3 ChoiceFields (country, state,city). On select of a country
from the dropdown, the valid states dropdown gets populated and on
select of a state the respective city dropdown gets populated.
For achieving the above functionality I am calling an ajax function
which returns me the available data of the other dropdown in Json Form
{stat_id, stat_name}
I am able to populate the data in next dropdown i.e state and city,
but when i click submit form it shows "Select a valid choice. 8 is not
one of the available choices"
8 happens to be the stat_id in this case.
I have gone through google but dint find anything useful. Help is
really appreciated.

Chris Cogdon

unread,
Dec 13, 2012, 7:39:04 AM12/13/12
to django...@googlegroups.com
How are you populating the choices in the ChoiceField? Remember, you need to do this on BOTH the POST AND the non-POST versions of creating the form.

But ChoiceField is a Form field, not a Model field... so its not perfectly clear what you're asking.

siddharth ghumre

unread,
Dec 14, 2012, 12:05:44 AM12/14/12
to django...@googlegroups.com
Hey Chris

Thanks for the reply....but i figured it out what was wrong.
:)
-Siddharth
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/NEBq73A9EyEJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Chris Cogdon

unread,
Dec 14, 2012, 6:52:22 PM12/14/12
to django...@googlegroups.com
Good... but we'd all appreciate knowing what was wrong, so we can guide others :)

vijay shanker

unread,
Feb 12, 2013, 6:14:12 AM2/12/13
to django...@googlegroups.com
hey siddhartha i have similar problem, i created dynamic option with ajax calls and getting the same error
"select a valid choice. xyz is not available choices , how did you solved it?"
Reply all
Reply to author
Forward
0 new messages