OOP and Django

Being a senior in college means many things. It means job interviews and upper-level classes, emotional instability and independent living. It also means countless hours of sitting in uninteresting classes whose sole purpose is to fulfill some graduation requirement. For me, that means lots of daydreaming--about anything other than that class. Recently however, during one daydream, I had a brain wave worth typing up: What's the deal with Object-Oriented Programming and Django?

The Convention

Browsing through the views.py file in just about any publicly-available Django-based application will almost certainly reveal nothing more than a bunch of functions. These functions are undeniably specialized: they take in an HttpRequest object (plus possibly some more information), and they return an HttpResponse object. Although these functions may be specialized, nevertheless they are still just functions.

This should come as no surprise to anyone who has used the framework--in fact, it's encouraged by common convention! Not only does the tutorial use plain functions for views, but also the Django Book, and just about every other application out there. The question now becomes "why"? Why, in a language that seems to be "objects all the way down", does a paradigm emerge for this domain (Django views) wherein functions are used almost exclusively in lieu of objects?

That's not entirely true, sir...

Any time a broad statement like "just about any" is used, the exceptions are what become interesting. The admin application (both newforms-admin and old) is probably the most notable and interesting exception to my earlier broad statement. It's interesting because it's Django's shining star! Other applications which use object orientation: databrowse and formtools. These are some great Django apps which use Object-Orientation in the views.

Looking at those apps which use OOP and those which don't reveals an interesting idea: those apps which strive to go above-and-beyond in terms of modularity tend to be those who end up using classes and their methods for views. Now this same functionality could be accomplished by using plain functions, but they haven't--their functionality was accomplished using classes and methods.

Please keep in mind that what I'm not trying to do is make a value judgement on Object-Oriented programming vs. functional programming vs. any other programming paradigm. Instead, I'm providing an observation about the emergence of a common practice, and trying to analyze its implications.

But wait!

What really is the difference between writing a plain function as a view and Object-Oriented programming? It's completely reasonable to argue that writing a plain function for a view is, in fact, Object-Oriented programming. All class methods take in self as their first positional argument, and all views take in request as their first positional argument. Taking in this argument allows access to state which would otherwise be difficult to access. Changing the order of urlpatterns is equivalent to changing the polymorphic properties of a class and dynamic method lookup.

In essence, one could argue that using a plain function as a view is strictly equivalent to writing a method on the HttpRequest object. Thinking about it in this way, writing a Django application is really nothing more than building up a monolithic HttpRequest object which the user can call different methods on using its API: the URL. To me, this is a really interesting idea!

Off My Rocker

This is the result of extreme classroom boredom--so maybe posts here will continue down this slightly-more-esoteric road for a while. But honestly this was an interesting thought-experiment, and I'd like to get some feedback on what people think as well. Am I totally off base with this analysis? Moreover, do you use true Python "classes" as your views? If so, what benefits does it bring to the table?

26 Comments So Far...

By troll at 7:16 a.m. on Feb. 9, 2008

Oop is good for complex situations. When you can find actually complex web app that would really benefit from oop all the way let us know. As far as I have seen there has been none so far.

 

By she at 2:35 p.m. on Feb. 9, 2008

OOP in general is good. Once you start to think about objects and they being able to solve the problem at hand, everything becomes better arranged. And I dont refer to C++ OOP although even C++ way in this regard is better than C's way. (But C++ has a lot of other problems)

I am using plenty of objects, some big some small.
The complexity has nothing to do with it, maybe except really trivial things like displaying hello world.

 

By Benjamin Golub at 3:11 p.m. on Feb. 9, 2008

Exactly; most Django apps are relatively simple and dont need OOP. I'm sure that EveryBlock probably does something more complex then function after function after function but for most applications it works well.

Check out http://www.rssmeme.com/ an app that I launched this week. Nothing more than 15 functions in a 161 line views.py

 

By miran at 2:46 p.m. on Feb. 9, 2008

When I develop Django apps, I usually have several packages in my main app and each one of those has a views.py
That way, you could think of the views as methods of a package object.
But all in all I don't think using OO (in the strict sense, e.g. inheritance, polymorphism, etc.) translates well to views in Django, because requests are basically function calls. Your browser calls some function of a web server. So I think having them as functions is the way to go and with that you don't introduce any unnecessary complexity.
And besides, I've rarely shared any implementation between my views so I don't see how packing them up in OO would benefit me.

 

By Jared at 9:57 p.m. on Feb. 9, 2008

Think about the actual objects in a Django application: the model objects, request and response objects, etc. Just because views aren't encapsulated like an Object doesn't mean they should be. An action is not a noun, like a view is not an object.

 

By Eric Florenzano at 10:03 p.m. on Feb. 9, 2008

Ooh, that's a really good point! Your argument is that a view is a verb, whereas a model is a noun? I guess I had forgotten about the "kingdowm of nouns" analogy from my OOP theory. I mostly agree.

 

By frits at 9:45 a.m. on Feb. 10, 2008

Try to do some basic polymorphism with Django model objects, and you will notice that Django models are not object oriented, but record oriented.

 

By Paul at 10:35 p.m. on Feb. 9, 2008

I've used OOP in python and even within the Django framework. Its important to make a distinction between the application of different design patterns. Trying to cram OOP into the Django framework itself is putting the cart in front of the horse unless you can give a use case.

What requirement is there for OOP in Django?

 

By Noah at 11:39 p.m. on Feb. 9, 2008

The thing is, a view isn't an object, strictly speaking it's not a noun. It's a process preformed on a noun to transform it into another noun.

Each view every time it is called operates on different instance of an object and returns a different instance of another object.

It's a processor and not re-used later in the code and doesn't have attirbutes to be altered and methods to be re-called consistently.

Although you could have a class who's methods get called (rather than a module with functions) there is little use for it to be a class in actual function because it doesn't require persistence within the chain of events leading from a request to a response.

So all the work of making an class and an instance is kind of futile because it's not going to continue to exist after the view is returned.

However there is benefit sometimes, but I wouldn't shoot for bing OO just because it's "better" in an academic sort of way.

That said, there are times when it's a good idea.

 

By Noah at 11:40 p.m. on Feb. 9, 2008

I wish I'd read some of the other comments before I pitched in.

 

By zzz at 4:42 a.m. on Feb. 10, 2008

Compare Pylons, which uses Controller classes for no apparent reason whatsoever.

 

By -- at 7:31 a.m. on Feb. 10, 2008

isn't a function in python a static method on the containing module object?

 

By wholesale jewelry at 12:37 a.m. on May 15, 2009

Good website,it is useful and helpful.

 

By ben10 oyunları at 3:01 a.m. on May 25, 2009

translates well to views in Django, because requests are basically function calls. Your browser calls some function of a web server.

 

By injection molding at 12:33 a.m. on June 14, 2009

thanks a lot for the post!

 

By Mike A at 5:42 a.m. on June 16, 2009

Django is weird in this respect and it misses out on so much whilst causing users to tap a lot more code in.

Thinking about it, not only are django views not objects but nor are they real functions (I'm thinking functions in the pure sense - no side effects) - cos they hit the database.

This 'false-function' design gets the worst of both worlds:-

You have to deal with security for each and every function. Ok, this might be just a decorator, but you still have to write it every time
Likewise you have to deal with rendering the response each and every time
You can't do things like put functions into the database - this would be great as permissions would be as simple as matching Groups to functions.
You end up starting to think that fat functions are normal.

Compare any two view functions and you're bound to find loads of similarities.

I find it especially ironic with all the song and dance they make about how DRY the django code base is.

As for django's Model 'objects', they aren't objects at all, but as rightly pointed out, data structures. Objects are about messages and behaviour, not attributes and queries.

This 'false-object' design has had a severe impact on the django code base, which has largely gone unnoticed and, I believe, is contributing to the problems they now face getting releases out the door. When was 1.1 supposed to be out?

For fun, grep for getters and you'll see what a sorry state django is in behind the scenes.

In fact, last time I counted there were actually more return statements than defs! Such things should never be used in OO code, outside of __new__.

Also, to correct some misconceptions, HTTP requests are in no way function calls. HTTP works by a browser sending a request message to a web server, which may then adjust its state and then may send a response message back. They are asynchronous messages to objects, the object being the web server. Even GETs work in this way. The fact that a GET request may respond with a different page at different time means it is not treated as a function in the true sense.

 

By WoW Power Leveling at 3:38 a.m. on June 18, 2009

Thx for sharing the exciting info with us.

 

By sexy costumes at 8:05 p.m. on June 22, 2009

This sounds a little bit like the idea of an "open" module in Ruby. I think that this is a really interesting idea, but one with some potentially dangerous consequences.

 

By jordan shoes at 3:55 a.m. on June 25, 2009

Good website,it is useful and helpful.

 

By jordan shoes at 3:55 a.m. on June 25, 2009

isn't a function in python a static method on the containing module object?

 

By ugg boots at 3:56 a.m. on June 25, 2009

Think about the actual objects in a Django application: the model objects, request and response objects, etc.

 

By nike shoes at 3:57 a.m. on June 25, 2009

Oop is good for complex situations. When you can find actually complex web app that would really benefit from oop all the way let us know. As far as I have seen there has been none so far.

 

By tiffany jewellery at 3:57 a.m. on June 25, 2009

Compare Pylons, which uses Controller classes for no apparent reason whatsoever.

 

By lingerie wholesale at 9:55 a.m. on June 28, 2009

I really appreciate it. Hope to know more important things regarding design from it.

 

By Drusilla at 1:34 a.m. on July 2, 2009

Good evening. It's all right to have butterflies in your stomach. Just get them to fly in formation.
I am from Somalia and also now teach English, tell me right I wrote the following sentence: "At first, when we are used to continual flea protection the thought of a daily flea medicine."

Thanks :p. Drusilla.

 

By hearthomes at 10:02 p.m. on July 3, 2009

this is very interesting. thanks for that. we need more sites like this.
http://hearthomes.blogbus.com/
http://blog.sina.com.cn/hearthomes09
http://hearthomes.yo2.cn/

 

Voice your opinion...