Skip to content

Instantly share code, notes, and snippets.

@lorne-luo
Last active May 8, 2018 13:29
Show Gist options
  • Save lorne-luo/2c8bb5756c2b9d76762126d9c5661b65 to your computer and use it in GitHub Desktop.
Save lorne-luo/2c8bb5756c2b9d76762126d9c5661b65 to your computer and use it in GitHub Desktop.
django inline formset
--- a/certification/forms.py
+++ b/certification/forms.py
@@ -309,3 +309,47 @@ class BaseCertRecipeIngredientFormSet(BaseFormSet):
+class CertRecipeAttachmentForm(forms.ModelForm):
+ # id = forms.CharField(label=_("id"), max_length=254, required=False, widget=forms.HiddenInput())
+ type = forms.ChoiceField(label=_("Attachment type"), choices=[('', '----')] + RECIPE_ATTACHMENT_TYPE_CHOICES,
+ required=True,
+ error_messages={'required': "Please select type."})
+ file = ExtFileField(label=_("File"), required=False,
+ ext_whitelist=['.pdf', '.doc', '.docx', '.jpg', '.png', '.gif', '.eps'],
+ help_text=mark_safe(
+ 'Type: jpg, png, gif, doc, docx, pdf, eps'))
+ salesforce_url = forms.URLField(label=_("salesforce url"), max_length=254, required=False,
+ widget=forms.HiddenInput())
+ salesforce_id = forms.CharField(label=_("salesforce id"), max_length=254, required=False,
+ widget=forms.HiddenInput())
+
+ class Meta:
+ model = CertRecipeAttachment
+ exclude = []
+
+
+CertRecipeAttachmentFormSet = inlineformset_factory(CertRecipe, CertRecipeAttachment, form=CertRecipeAttachmentForm,
+ extra=1, max_num=10)
--- a/certification/templates/certification/recipe/launch_detail.html
+++ b/certification/templates/certification/recipe/launch_detail.html
@@ -16,7 +16,50 @@
{% block recipe_images %}
+ <table id="attachments-table">
+ {{ certrecipeattachment_set.management_form }}
+ <thead>
+ <tr>
+ <th><label>Type</label></th>
+ <th><label>File</label></th>
+ <th><label></label></th>
+ </tr>
+ </thead>
+ <tbody id="attachments-table-content">
+ {% for form in certrecipeattachment_set.forms %}
+ <tr class="{% cycle row1 row2 %} formset_row">
+ <td class="{% if form.type.errors %}error{% endif %}">
+ {% for hidden in form.hidden_fields %}
+ {{ hidden }}
+ {% endfor %}
+ {{ form.type }}
+ {% if form.type.errors %}
+ <span class="error-text" style="margin-left: 0;">{{ form.type.errors.as_text }}</span>
+ {% endif %}
+ </td>
+ <td class="{% if form.file.errors %}error{% endif %}">
+ {% include "helper_templates/inline_cert_image_field.html" with field=form.file remote_image_url=form.salesforce_url.value object=object %}
+ </td>
+ <td>
+ {{ form.DELETE }}
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
</div>
+
+ <script src="https://raw.githubusercontent.com/elo80ka/django-dynamic-formset/master/src/jquery.formset.js"></script>
+ <script type="text/javascript">
+ $('#attachments-table-content .formset_row').formset({
+ addText: 'Add another',
+ prefix: 'certrecipeattachment_set',
+ addCssClass: 'btn-blue-form',
+ deleteCssClass:'remove-recipe-ingredient',
+ formCssClass: 'dynamic-form',
+ deleteText: '<img src="{% static 'images/svg/trash.svg' %}">',
+ });
+ </script>
{% endblock %}
--- a/certification/views_item.py
+++ b/certification/views_item.py
@@ -509,9 +509,22 @@ class CertRecipeAddEditView(CertItemAddEditView):
class CertRecipeLaunchDetailView(CertRecipeAddEditView):
+ def get_context_data(self, **kwargs):
+ context = super(CertRecipeLaunchDetailView, self).get_context_data(**kwargs)
+ if self.request.POST:
+ context['certrecipeattachment_set'] = CertRecipeAttachmentFormSet(self.request.POST, self.request.FILES, instance=self.object)
+ else:
+ context['certrecipeattachment_set'] = CertRecipeAttachmentFormSet(instance=self.object)
+
+ context['is_multipart'] = True
+ return context
def form_valid(self, form):
context = self.get_context_data()
+ certrecipeattachment_set = context['certrecipeattachment_set']
+ certrecipeattachment_set.instance = form.instance()
+ if not form.is_valid() or not certrecipeattachment_set.is_valid():
+ return self.render_to_response(self.get_context_data(form=form, formset=certrecipeattachment_set))
+
+ certrecipeattachment_set.save()
return super(CertRecipeLaunchDetailView, self).form_valid(form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment