Last active
May 8, 2019 01:35
-
-
Save lorne-luo/3cc2fcd57103111f324f63fef3beb0b1 to your computer and use it in GitHub Desktop.
django inline formset version2
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: | |
+ self.fields.get(field_name).widget.attrs['class'] = 'form-control' | |
+ | |
+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">对应肤质阐述</h3> | |
+ </div> | |
+ | |
+ <div class="box-body form-horizontal"> | |
+ <table id="attachments-table" class="col-xs-12"> | |
+ {{ productanalysis_formset.management_form }} | |
+ <thead> | |
+ <tr> | |
+ {% 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> | |
+ {{ form.sensitive_type }} | |
+ {{ form.sensitive_type.error }} | |
+ </td> | |
+ <td> | |
+ {{ form.pigment_type }} | |
+ {{ form.pigment_type.error }} | |
+ </td> | |
+ <td> | |
+ {{ form.loose_type }} | |
+ {{ form.loose_type.error }} | |
+ </td> | |
+ <td> | |
+ {{ form.analysis }} | |
+ {{ form.analysis.error }} | |
+ </td> | |
+ <td> | |
+ {{ form.product }} | |
+ {{ form.id }} | |
+ {{ form.DELETE }} | |
+ </td> | |
+ </tr> | |
+ {% endfor %} | |
+ </tbody> | |
+ </table> | |
+ </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 add-{{ productanalysis_formset.prefix }}', | |
+ deleteCssClass: 'btn remove-{{ productanalysis_formset.prefix }}', | |
+ formCssClass: 'dynamic-form-{{ productanalysis_formset.prefix }}', | |
+ deleteText: '<i style="color:#dd4b39" class="fa fa-lg fa-trash"></i>', | |
+ added: function () { | |
+ // added callback | |
+ // init chosen select | |
+ } | |
+ }); | |
+</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() | |
+ productanalysis_formset = ProductAnalysisFormSet(self.request.POST, self.request.FILES, | |
+ prefix='productanalysis_formset', instance=self.object) | |
+ productanalysis_formset.instance = self.object | |
+ if productanalysis_formset.is_valid(): | |
+ productanalysis_formset.save() | |
+ else: | |
+ messages.error(self.request, str(productanalysis_formset.errors)) | |
+ return super(ProductAddView, self).form_invalid(form) | |
return HttpResponseRedirect(self.get_success_url()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment