Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design of CBVs in Django

Tags:

python

django

I'm currently trying to get into "Class Based Views" with Django 1.5.

From the design perspective i wonder where to put the logic to process data comming from a form in a simple FormView.

I know that all form validation code comes into the method form_valid(). But where to put things which processes data of the form. I read that its somehow inappropriate to put too much logic into the form_valid() method.

There are the get(), post(), get_context_data(), head(), etc. methods... which should I use in which case?

like image 607
Jurudocs Avatar asked Nov 04 '22 03:11

Jurudocs


2 Answers

  1. Form validation, data cleaning, etc goes with the form class in the clean methods

  2. Processing of a valid form should go in an overridden form_valid method

That's it! If your use-case is more complicated you can call out to other methods of your creation from form_valid...

like image 168
David Wheaton Avatar answered Nov 13 '22 16:11

David Wheaton


Any answer to this question is open for discussion. That said, views are just Python classes, so you could overwrite any method to customize things accordingly.

It is also perfectly legit to create an extra method on your class to handle data processing.

like image 23
Hedde van der Heide Avatar answered Nov 13 '22 17:11

Hedde van der Heide