Hi, I would like to add a field to a form dynamically. I'm using Django
1.0.2.

Here's my view code:

class CarForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(CarForm, self).__init__(*args, **kwargs)
        self.fields['extraField'] = forms.CharField()

    class Meta:
        model = Car

class CarPhotoForm(ModelForm):
    class Meta:
        model = CarPhoto

def car(request, template_name = 'cars.html'):
    carForm = CarForm(True)
    carPhotoForm = CarPhotoForm()
    return render_to_response(template_name,  {'carForm' : carForm,
'carPhotoForm' :  carPhotoForm})


And here's  a model:

class Car(models.Model):
    car_type = models.CharField(max_length = 50)

class CarPhoto(models.Model):
    photo = models.ImageField( upload_to = 'cars_photos/')
    car = models.ForeignKey(Car)


The template is very simple too:

<h4>Car form:</h4>
<form id="carForm" method="POST" action=".">
    {{carForm.as_p}}
    <input type="Submit" value="Add car"/>
</form>
<h4>Car photo form:</h4>
<form id="carPhotoForm" method="POST" action=".">
    {{carPhotoForm.as_p}}
    <input type="Submit" value="Add photo"/>
</form>

I took this idea from
http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/.  When I try to view
the page, when there's my own __init__ method in CarForm class, then the car
form doesn't contain any field. When i remove the __init__ method,
everything works correctly.  How to add a field to a form during a runtime ?

Thanks in advance,

Marek

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