Last active
May 22, 2018 23:10
-
-
Save lorne-luo/decd9124b65d2fd74affc281e63754c5 to your computer and use it in GitHub Desktop.
django inline formset with jquery.formset.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- b/apps/product/forms.py | |
+ | |
+class ProductAnalysisInlineForm(forms.ModelForm): | |
+ class Meta: | |
+ model = ProductAnalysis | |
+ fields = '__all__' | |
+ | |
+ def __init__(self, *args, **kwargs): | |
+ super(ProductAnalysisInlineForm, self).__init__(*args, **kwargs) | |
+ for field_name in self.fields: | |
+ field = self.fields.get(field_name) | |
+ field.widget.attrs['class'] = 'form-control' | |
+ | |
+ self.fields['product'].widget = forms.HiddenInput() | |
+ | |
+ | |
+ProductAnalysisFormSet = inlineformset_factory(Product, ProductAnalysis, form=ProductAnalysisInlineForm, | |
+ can_order=False, can_delete=True, extra=1) | |
+ | |
--- b/apps/product/templates/product/product_form.html | |
+ | |
+<div class="box box-info"> | |
+ <div class="box-header with-border"> | |
+ <h3 class="box-title">title</h3> | |
+ </div> | |
+ | |
+ <div class="box-body form-horizontal"> | |
+ <table id="attachments-table" class="col-xs-12"> | |
+ {{ productanalysis_formset.management_form }} | |
+ <thead> | |
+ <tr> | |
+ <th></th> | |
+ {% with productanalysis_formset.forms|first as first_productanalysis_form %} | |
+ {% for field in first_productanalysis_form.visible_fields %} | |
+ <th><label>{{ field.label }}</label></th> | |
+ {% endfor %} | |
+ {% endwith %} | |
+ <th></th> | |
+ </tr> | |
+ </thead> | |
+ <tbody id="{{ productanalysis_formset.prefix }}_table"> | |
+ {% for form in productanalysis_formset.forms %} | |
+ <tr class="{% cycle row1 row2 %} formset_row"> | |
+ <td> | |
+ {% for hidden in form.hidden_fields %} | |
+ {{ hidden }} | |
+ {% endfor %} | |
+ </td> | |
+ {% for field in form.visible_fields %} | |
+ <td class="{% if form.type.errors %}error{% endif %}"> | |
+ {{ field }} | |
+ </td> | |
+ {% endfor %} | |
+ </tr> | |
+ {% endfor %} | |
+ </tbody> | |
+ </table> | |
+ </div> | |
+ <div class="box-footer"> | |
+ <button type="submit" style="width:108px" class="btn btn-primary">Save</button> | |
+ <button type="submit" style="width:108px" class="btn btn-primary" name="_continue">Save & Continue</button> | |
+ </div> | |
+</div> | |
+ | |
+<script src="{% static 'js/jquery.formset.js' %}"></script> | |
+<script type="text/javascript"> | |
+ $('#{{ productanalysis_formset.prefix }}_table .formset_row').formset({ | |
+ addText: 'add new', | |
+ prefix: '{{ productanalysis_formset.prefix }}', | |
+ addCssClass: 'btn btn-success', | |
+ deleteCssClass: 'remove-recipe-ingredient', | |
+ formCssClass: 'dynamic-form', | |
+ deleteText: '<i style="color:#dd4b39" class="fa fa-lg fa-trash"></i>', | |
+ added: function () { | |
+ // added callback | |
+ } | |
+ }); | |
+</script> | |
+ | |
--- b/apps/product/views.py | |
def get_context_data(self, **kwargs): | |
context = super(ProductAddView, self).get_context_data(**kwargs) | |
+ if self.request.POST: | |
+ context['productanalysis_formset'] = ProductAnalysisFormSet(self.request.POST, self.request.FILES, | |
+ prefix='productanalysis_formset', | |
+ instance=self.object) | |
+ else: | |
+ context['productanalysis_formset'] = ProductAnalysisFormSet(prefix='productanalysis_formset', | |
+ instance=self.object) | |
return context | |
def form_valid(self, form): | |
self.object = form.save(commit=False) | |
context = self.get_context_data() | |
+ productanalysis_formset = context['productanalysis_formset'] | |
+ productanalysis_formset.instance = form.instance | |
+ if productanalysis_formset.is_valid(): | |
+ productanalysis_formset.save() | |
return super(ProductAddView, self).form_valid(form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment