DISQUS

Eric Florenzano's Blog: OOP and Django

  • troll · 1 year ago
    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.
  • she · 1 year ago
    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.
  • miran · 1 year ago
    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.
  • Benjamin Golub · 1 year ago
    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
  • Jared · 1 year ago
    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.
  • Eric Florenzano · 1 year ago
    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.
  • Paul · 1 year ago
    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?
  • Noah · 1 year ago
    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.
  • Noah · 1 year ago
    I wish I'd read some of the other comments before I pitched in.
  • zzz · 1 year ago
    Compare Pylons, which uses Controller classes for no apparent reason whatsoever.
  • -- · 1 year ago
    isn't a function in python a static method on the containing module object?
  • frits · 1 year ago
    Try to do some basic polymorphism with Django model objects, and you will notice that Django models are not object oriented, but record oriented.
  • wholesale jewelry · 7 months ago
    Good website,it is useful and helpful.
  • ben10 oyunları · 7 months ago
    translates well to views in Django, because requests are basically function calls. Your browser calls some function of a web server.
  • injection molding · 6 months ago
    thanks a lot for the post!
  • Mike A · 6 months ago
    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.
  • WoW Power Leveling · 6 months ago
    Thx for sharing the exciting info with us.
  • sexy costumes · 6 months ago
    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.
  • jordan shoes · 6 months ago
    Good website,it is useful and helpful.
  • jordan shoes · 6 months ago
    isn't a function in python a static method on the containing module object?
  • ugg boots · 6 months ago
    Think about the actual objects in a Django application: the model objects, request and response objects, etc.
  • nike shoes · 6 months ago
    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.
  • tiffany jewellery · 6 months ago
    Compare Pylons, which uses Controller classes for no apparent reason whatsoever.
  • lingerie wholesale · 6 months ago
    I really appreciate it. Hope to know more important things regarding design from it.
  • Drusilla · 6 months ago
    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.
  • hearthomes · 6 months ago
    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/
  • plastic injection molding · 6 months ago
  • Mengembalikan Jati Diri Bangsa · 6 months ago
    Great info, thanks for sharing
  • stop dreaming start action · 6 months ago
    good written
  • stop dreaming start action · 6 months ago
    very clear
  • Free Classified Ads · 5 months ago
    Can you say any other Django alternatives? It's really interesting, and i'm wondering if there is something more like that :)
  • tiffany and co · 5 months ago
    It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader.Thanks again.
  • koxp · 5 months ago
    It is a wonderful article,i like it,thank you very much!
  • film izle · 5 months ago
    thanks man
  • sohbet · 5 months ago
    that's a great post, thank you!
  • radyo dinle · 5 months ago
    that's a great post, thank you!
  • Ed Hardy shirts · 5 months ago
    that's a great post, thank you!