Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding fields to a Django form dynamically with JavaScript & JSONField

I have a model form in which I need to store an unknown number of helpers alongside a thing. The names can be serialised upon save, and that's not a problem. It's being able to clean and validate them upon submission.

The form looks like;

class ThingForm(forms.ModelForm):
    """
    Form for the Thing
    """
    owner = forms.CharField()
    helpers = forms.CharField()

    class Meta:
        model = Thing

    def save(self, *args, **kwargs):
        """
        Serialize helpers to JSON.
        """
        ...

And the model is using a JSONField to store the serialised helpers.

class Thing(models.Model):
    owner = models.CharField()
    helpers = JSONField()

I have JavaScript adding as many helpers as required with the same input name:

<input name="helpers" value="Fred" />
<input name="helpers" value="Joe" />

Which is returning a tuple of the helpers. The problem is that the if the form isn't valid - those names will be lost and the cleaning isn't working.

My first thought was to add to the form's constructor:

    def __init__(self, *args, **kwargs):
        super(ThingForm, self).__init__(*args, **kwargs)
        try:
            helpers = args[0].pop('helpers')
            for name in helpers:
                # Add a charfield, or something?
        except:
            pass

But I'm not really getting anywhere...

like image 497
Wil Avatar asked Nov 20 '25 18:11

Wil


1 Answers

Thanks to AdamKG for the answer to this. You can just use the list in your view again:

View:

if request.method == 'POST':
    helpers = request.POST.getlist('helpers')
    form = ThingForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/saved/')
else:
    helpers = None
    form = ThingForm()

return render_to_response('my_template.html',
    {'helpers': helpers, 'form': form},
    context_instance=RequestContext(request))

Template:

{% for field in form %}
    {% if field.name == 'helpers' %}
        {% for name in helpers %}
            <input name="helpers" value="{{ name }}" />
        {% endfor %}
    {% else %}
        {{ field }}
    {% endif %}
{% endfor %}
like image 190
Wil Avatar answered Nov 23 '25 08:11

Wil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!