DISQUS

Eric Florenzano's Blog: Exploring Mixins with Django Model Inheritance

  • Orestis Markou · 1 year ago
    You might want to split up the double Meta: abstract = True in the last example.

    Also, the DateAddedMixin is referenced in the first example, but it's not defined yet.

    Good ideas overall! Model inheritance looks very good, I can see lots of "plugins" being created, in the :acts_as_* fashion that Rails has...
  • Eric Florenzano · 1 year ago
    Whoops! Good catches, thanks. Fixing now.
  • Thierry Schellenbach · 1 year ago
    I came from a Symfony background and am glad to see this kind of functionality in Django :)
    It is quite similar in purpose to propel behaviors for Symfony :)
  • sean · 1 year ago
    model inheritance would really help me a lot in designing models!
  • mb0 · 1 year ago
    what about adding another method datemixin_save that is called by default? clients can override the save method and only need to call the mixin save method of the parent class... easy enough, but not tested.
  • Eric Flo · 1 year ago
    You know, that's a really great idea. This could be done automatically with a small patch to Model.save(). Maybe I'll write up a patch later!
  • kevin · 1 year ago
    eric, great post. looking forward to some interesting implementations in the future.

    also, we're talking model inheritance this week at django-nyc meetup, and we now have another great reference/discussion point.

    cheers!
  • Anonymous · 1 year ago
    Could someone show how to use the DateMixin alone?
  • Alex · 1 year ago
    You just subclass it alone, the behavior is self contained.
  • Carl Meyer · 1 year ago
    You don't give the DateMixin enough credit. I've been using something very much like that in production ever since qs-rf merged. Anytime any model overrides save() it should use super() to call the default save() from models.Model. If that model is now a subclass of DateMixin instead of Model, the identical super() call will call DateMixin.save(), which will in turn call Model.save() - in other words, it all just works. The subclass doesn't need to do anything different than it would if subclassing Model normally.
  • hachaboob · 1 year ago
    Nice example! An excellent way to stay DRYer in the model.
  • anon · 1 year ago
    BlogPost.tree.get_roots() generates an attribute error for me on django revision 7538, anybody experiencing this...

    AttributeError: 'Options' object has no attribute '_join_cache' happens in

    '''
    /usr/local/lib/python2.5/site-packages/django/db/models/sql/query.py in setup_joins(self, names, opts, alias, dupe_multis, allow_many, allow_explicit_fk, can_reuse)
    1121 opts.pk.column), exclusions=joins)
    1122 joins.append(alias)
    -> 1123 cached_data = opts._join_cache.get(name)
    1124 orig_opts = opts
    1125
    '''
  • anon · 1 year ago
    BlogPost.tree.get_roots() does work when tree = NaiveHierarchyManager() is defined under BlogPost
  • Eric Florenzano · 1 year ago
    Yes, this a result of the manager which doesn't yet work with Django trunk. I still suggest checking out

    http://code.djangoproject.com/ticket/7252
    and http://code.djangoproject.com/ticket/7154
  • Dewayne Curry · 1 year ago
    undersleep piaffe scotomata methine anisotropism seres antihysteric unpoisonable
    <a href= http://freebizadsweb.com/tablemaker/tablemaker.asp >Button Maker
    http://www.cleversley.com/
  • Jeffery Witt · 1 year ago
    undersleep piaffe scotomata methine anisotropism seres antihysteric unpoisonable
    <a href= http://www.theoxfordgroup.com/ >The Oxford Group
    http://www.winspec.com/
  • Rosalie Dunlap · 1 year ago
    undersleep piaffe scotomata methine anisotropism seres antihysteric unpoisonable
    <a href= http://www.creative.com.cy/ >Creative Tours
    http://www.thisisthenortheast.co.uk/the_north_e...
  • ben 10 oyunları · 7 months ago
    You know, that's a really great idea. This could be done automatically with a small patch to Model.save(). Maybe I'll write up a patch later!
  • wholesale jewelry · 7 months ago
    Nice blog.thanks for your sharing.
  • jimmy poiy · 7 months ago
    nike tip, thanks a lot.
  • wow power leveling · 6 months ago
    Great work, u can say it again.
  • wholesale Lingerie · 6 months ago
    Web developers often use caches off the database due to a few aspects inherent with databases. Computing values from a normalized database takes time, and so it's a good target for memorization. Even if it's cheap to compute or pull at the database
  • jordan shoes · 6 months ago
    undersleep piaffe scotomata methine anisotropism seres antihysteric unpoisonable
  • jordan shoes · 6 months ago
    You know, that's a really great idea. This could be done automatically with a small patch to Model.save(). Maybe I'll write up a patch later!
  • ugg boots · 6 months ago
    Great work, u can say it again.
  • nike shoes · 6 months ago
    nike tip, thanks a lot.
  • tiffany jewellery · 6 months ago
    Nice blog.thanks for your sharing.
  • lingerie wholesale · 6 months ago
    I think you will make these projects into a success also!
  • air jordans · 5 months ago
    It is a wonderful article,i like it,thank you very much!
  • Free Classified Ads · 5 months ago
    I'm interested about the traffic. What if i load up my server with much of this code, and many visitors will visit it. Won't it crash if my server is not very new?
  • links of london · 5 months ago
    Thanks for your comments!
  • koxp · 5 months ago
    Thanks for the great tutorial.