Commits

Mark Lavin committed fc998da

Add working dynamic formset example.

  • Participants
  • Parent commits b81b986

Comments (0)

Files changed (6)

File example/core/forms.py

 from django import forms
+from django.forms.models import modelformset_factory
 from django.contrib.localflavor.us.forms import USStateField, USStateSelect
 
 import selectable.forms as selectable
 
 from example.core.lookups import FruitLookup, CityLookup
+from example.core.models import Farm
 
 
 class FruitForm(forms.Form):
     )
     state = USStateField(widget=USStateSelect, required=False)
 
+
+class FarmForm(forms.ModelForm):
+
+    class Meta(object):
+        model = Farm
+        widgets = {
+            'fruit': selectable.AutoCompleteSelectMultipleWidget(lookup_class=FruitLookup),
+        }
+
+
+FarmFormset = modelformset_factory(Farm, FarmForm)
+

File example/core/lookups.py

     model = Fruit
     search_field = 'name__icontains'
 
-
 registry.register(FruitLookup)
 
 

File example/core/templates/base.html

 {% block extra-js %}{% endblock %}
 </head>
     <body>
+        {% block content %}
         <form action="" method="post" >
             {% csrf_token %}
             {% if form.is_valid %}
             {% endif %}
             <button>Submit</button>
         </form>
+        {% endblock %}
     </body>
 </html>

File example/core/templates/formset.html

+{% extends "base.html" %}
+
+{% block extra-css %}
+{{ formset.media.css }}
+{% endblock %}
+
+{% block extra-js %}
+<script type="text/javascript" src="http://django-dynamic-formset.googlecode.com/svn/trunk/src/jquery.formset.min.js"></script>
+<script type="text/javascript">
+    $(document).ready(function() {
+        $('#id_farm_table tbody tr').formset({
+            prefix: '{{ formset.prefix }}',
+       		added: function(row) {
+       			bindSelectables($(row));
+       		}	               		
+        });
+    });
+</script>
+{{ formset.media.js }}
+{% endblock %}
+
+
+{% block content %}
+    <form action="" method="post" >
+        {% csrf_token %}
+        <table id="id_farm_table" border="0" cellpadding="0" cellspacing="5">
+            <thead>
+                <tr>
+                    <th scope="col">Name</th>
+                    <th scope="col">Owner</th>
+                    <th scope="col">Fruit</th>
+                </tr>
+            </thead>
+            <tbody>
+                <tr>{{ formset.management_form }}</tr>
+                {% for form in formset.forms %}
+                <tr id="{{ form.prefix }}-row">
+                    <td>{{ form.name }}</td>
+                    <td>{{ form.owner }}</td>
+                    <td>{{ form.fruit }}</td>
+                </tr>
+                {% endfor %}
+            </tbody>
+        </table>
+        <button>Submit</button>
+    </form>
+{% endblock %}

File example/core/urls.py

 
 
 urlpatterns = patterns('example.core.views',
+    url(r'^formset/', 'formset', name='example-formset'),    
     url(r'^advanced/', 'advanced', name='example-advanced'),
     url(r'^', 'index', name='example-index'),
 )

File example/core/views.py

 from django.shortcuts import render_to_response
 from django.template import RequestContext
 
-from example.core.forms import FruitForm, ChainedForm
+from example.core.forms import FruitForm, ChainedForm, FarmFormset
 
 
 def index(request):
             form = ChainedForm()
 
     return render_to_response('advanced.html', {'form': form}, context_instance=RequestContext(request))
+
+
+def formset(request):
+
+    if request.method == 'POST':
+        formset = FarmFormset(request.POST)
+    else:
+        if request.GET:
+            formset = FarmFormset(initial=request.GET)
+        else:
+            formset = FarmFormset()
+
+    return render_to_response('formset.html', {'formset': formset}, context_instance=RequestContext(request))