Dynamic Formset Radiobutton Issue

55 views
Skip to first unread message

Jur Remeijn

unread,
Oct 23, 2015, 8:54:49 AM10/23/15
to Django users
So I'm using a dynamic formset within django: https://github.com/elo80ka/django-dynamic-formset
I'm using it to render a page with a form (say, a pizza) and a formset (say, a set of toppings). Because the number of toppings isn't determined, I want to use a dynamical formset. 
Right now, I have the page display one instance of topping form, with a button to add another instance, which is added with the javascript found in the github repo (and as a attachment).
Only I have a problem: When I fill in the form for one topping, and then add another topping, all the radiobuttons get reset so the data from the first topping is lost. 

Does anyone see what goes wrong here? This is my code, simplified (I actually have a lot more fields for the form and subset, all choicefields needing radiobuttons, so this is quite a big issue for me). 

models.py:
class Pizza(models.Model):
    name= models.CharField(max_length=55)
    pricerange=models.CharField(max_length=55, choices=PRICERANGE_CHOICES)

class Topping(models.Model):
    pizza = models.ForeignKey(Pizza)
    number = models.IntegerField(null=True, blank=True)
    name=models.CharField(max_length=55, choices=TOPPINGNAME_CHOICES)

forms.py:
class PizzaForm(forms.ModelForm):

  class Meta:
    model = Pizza
    fields = ['name', 'pricerange']
    
  pricerange= forms.ChoiceField(required=True,widget=forms.RadioSelect(),choices=PRICERANGE_CHOICES)

class ToppingForm(forms.ModelForm):
  class Meta:
    model = Topping
    fields = ['number', 'name'
]
  
  number = forms.IntegerField(required=True)
  name = forms.ChoiceField(required=True,widget=forms.RadioSelect(),choices=TOPPINGNAME_CHOICES)

ToppingFormset = formset_factory(ToppingForm, max_num=8)

form.html:
<script type="text/javascript" src="/static/pizza/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/static/pizza/jquery.formset.js"></script>
<script type="text/javascript">
    $(function() {
        $('.ToppingForm').formset();
    })
</script>

<style type="text/css">
    .add-row {
        padding-left:18px;
        background:url(/static/arts/images/add.png) no-repeat left center;
    }
    .delete-row {
        display:block;
        margin:6px 0 0 0;
        padding-left:18px;
        background:url(/static/arts/images/delete.png) no-repeat left center;
    }
    .dynamic-form { padding: 5px 15px; }
</style>

<form method="post" action="">   {% csrf_token %}
{{ form.non_field_errors }}
        {{ form.name }}
        {{ form.pricerange}}

<div class="ToppingFormset">
{% for form in formset.forms %}
<div id=" {{ form.prefix }}-entry" class="ToppingForm">
<table class="t03" style="width:100%">
<tr><td colspan="8">{{ form.name.label_tag }} {{ form.name.errors }}</td></tr>
<tr>
{% for radio in form.name %}
<td>{{ radio }}</td>
{% endfor %}
</tr>
</table>
                                       {{ form.number.errors }}
{{ form.number}}
</div>
{% endfor %}
</div>
<p>{{ formset.management_form }}</p>

<input type="submit" value="Volgende"/>
</form>
jquery.formset.js

Collin Anderson

unread,
Oct 24, 2015, 2:01:36 PM10/24/15
to Django users
Hello,

That formset jQuery script it quite ancient (2009). You could try using jQuery 1.2.6 and see if it works correctly with that version.

Collin

Jur Remeijn

unread,
Nov 4, 2015, 10:48:58 AM11/4/15
to django...@googlegroups.com
Hello Collin, 

Thanks for your reply! I tried using jQuery 1.2.6. but it sadly didn't help my problem. Since the plugin is still updated regularly, I think using the newest jQuery plugin shouldn't be a problem. 

The weird thing is, today I was playing around with the problem, and it turns out that only the first instance of my formset resets. When I fill in a second one and add another, it doesn't reset! 

Still doesn't solve the problem though...

Jur.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/XwE4NqYYyII/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4881d596-f1f5-4182-8124-7c5ff2e0e5fe%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Jur Remeijn

unread,
Nov 4, 2015, 11:22:56 AM11/4/15
to Django users
Hi,

So I may have judged too fast. Turns out you were pretty close! I saw that the contributors to the plugin used jQuery in their demo project and I tried it as well... And it worked! 

Thanks for the suggestion! Really helped me out here :)

Op zaterdag 24 oktober 2015 20:01:36 UTC+2 schreef Collin Anderson:
Reply all
Reply to author
Forward
0 new messages