-
Website
http://www.eflorenzano.com/ -
Original page
http://www.eflorenzano.com/blog/post/oop-and-django/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
traviscooper
1 comment · 2 points
-
pppoll37
7 comments · 1 points
-
linhanyi
2 comments · 1 points
-
dobrych
2 comments · 3 points
-
bosveld
1 comment · 1 points
-
-
Popular Threads
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.
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.
Check out http://www.rssmeme.com/ an app that I launched this week. Nothing more than 15 functions in a 161 line views.py
What requirement is there for OOP in Django?
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.
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.
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.
http://hearthomes.blogbus.com/
http://blog.sina.com.cn/hearthomes09
http://hearthomes.yo2.cn/
http://www.direct-scaffold.com scaffolding
http://www.jsymq.com
http://www.zwf345.cn