Greetings,
There's a model with few ForeignKey fields:
class Alert(models.Model):
name = models.CharField()
sms = models.BooleanField()
email = models.BooleanField()
state = models.ForeignKey(State..)
city = models.ForeignKey(City)
When user is browsing the "X" state, when creating a new alert I only
want
to have cities from state "X" displayed in the form. (For both new &
existing records).
In my view, I know what state is the user exploring, and set it after
form validation (using commit=false).
So far I've been doing this by specifying the BaseForm in
form_for_model and form_for_instance.
views.py:
state = State.objects.get(name=state_name) # grabbed from urls.py
AlertForm = form_for_model(Alert, form=AlertBaseForm)
if request.method == "POST":
form = AlertForm(state, request.POST):
.....
forms.py:
class AlertBaseForm(BaseForm):
def __init__(self, state, *args, **kwargs):
super(AlertBaseForm, self).__init__(*args, **kwargs)
self.fields['city'].choices = [(c.id,c.name) for c in
City.objects.filter(state=state)]
I would like to rewrite the code to ModelForm, but I have no idea on
how to do this with ModelForm.
If anyone could share the example.
Thanks,
Robert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---