Skip to content

Instantly share code, notes, and snippets.

@arycloud
Last active November 12, 2023 18:07
Show Gist options
  • Save arycloud/fe7b85d2075ba32b871c8dee9b55cf31 to your computer and use it in GitHub Desktop.
Save arycloud/fe7b85d2075ba32b871c8dee9b55cf31 to your computer and use it in GitHub Desktop.
You can create the payload for your Inline Formset Factory forms and utilise to Unit Test your Django forms.
def build_formset_form_data(self, form_number, **data):
form = {}
for key, value in data.items():
form_key = f"form-{form_number}-{key}"
form[form_key] = value
return form
def build_formset_data(self, forms, **common_data):
formset_dict = {
"form-TOTAL_FORMS": f"{len(forms)}",
"form-MAX_NUM_FORMS": "1000",
"form-INITIAL_FORMS": "1"
}
formset_dict.update(common_data)
for i, form_data in enumerate(forms):
form_dict = self.build_formset_form_data(form_number=i, **form_data)
formset_dict.update(form_dict)
return formset_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment