On Mon, 2008-02-18 at 04:38 -0800, Robert wrote: [...] > 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.
ModelForms are just Forms underneath. All the magic happens in their __new__ metaclass method. So you should be able do something similar to this in the __init__ mtehod of your ModelForm subclass. Call the standard __init__ method first and then you'll get back something that is the same as what you had earlier. ModelForms are a complete change from what form_for_*() provided. It's just a way to unify the two cases and make it easier to add features (via extra methods on the ModelForm-derived classes). 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 -~----------~----~----~----~------~----~------~--~---