Skip to content

Instantly share code, notes, and snippets.

@mattparrilla
Forked from joeydi/views.py
Last active June 12, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattparrilla/210d05850d16d8053b781af187f61783 to your computer and use it in GitHub Desktop.
Save mattparrilla/210d05850d16d8053b781af187f61783 to your computer and use it in GitHub Desktop.
Form/FormSet cleaned_data handling
cleaned_data = (
[self.process_cleaned_data(form_data) for form_data in cleaned_data] if isinstanced(cleaned_data, list)
else self.process_cleaned_data(cleaned_data)
)
@mattparrilla
Copy link
Author

Probably less readable

@joeydi
Copy link

joeydi commented Jun 12, 2016

So, I almost want to put this array/object login inside of process_cleaned_data, since it's part of the process.

def process_cleaned_data(self, cleaned_data):
    if isinstance(cleaned_data, list):
        return [self.process_cleaned_data(form_data) for form_data in cleaned_data
                if bool(form_data)]

    # Do my cleaned_data processing

    return cleaned_data

def post(self, request, *args, **kwargs):
    context = self.get_context_data(**kwargs)
    all_forms_valid = all(context[form.__name__].is_valid() for form in self.form_list)

    if (all_forms_valid):
        # Save forms to session
        for form in self.form_list:
            cleaned_data = context[form.__name__].cleaned_data
            cleaned_data = self.process_cleaned_data(cleaned_data)
            request.session[self.session_key][form.__name__] = cleaned_data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment