One thing I've been experimenting with is model Mixins. For example, the aim is to create small abstract classes that are each focused around a particular function. These abstract classes can then be added to arbitrary models to apply those functions to models as desired.
For example, say I define a RatingsFields abstract class and a TrackingFields abstract class. These abstract classes can be mixed into any other model that we wish to add rating or tracking functionality to.
core/mixins.py
Since we applied the abstract classes to the Post model below, the model now has rating and tracking capabilities. This is useful to help simplify code where a lot of models share fields or methods with the same function.
myapp/models.py
Comments welcome.
Joe
For example, say I define a RatingsFields abstract class and a TrackingFields abstract class. These abstract classes can be mixed into any other model that we wish to add rating or tracking functionality to.
core/mixins.py
from djangoratings.fields import RatingField # 3rd party module class RatingFields(models.Model): rating = RatingField(range=5) # 5 possible rating values, 1-5 class Meta: abstract = True class TrackingFields(models.Model): deleted_on = models.DateTimeField(blank=True, null=True) created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) class Meta: abstract = True
Since we applied the abstract classes to the Post model below, the model now has rating and tracking capabilities. This is useful to help simplify code where a lot of models share fields or methods with the same function.
myapp/models.py
from core import mixins as core_mixins
class Post(core_mixins.TrackingFields, core_mixins.RatingFields):
name = models.CharField(max_length=128)
slug = models.SlugField(max_length=128)
...
Comments welcome.
Joe
Did you try django.contrib.contenttypes?
ReplyDeleteThanks for your comment, Pavel. I've used content types and generic relations in the past and they are very useful.
ReplyDeleteWith this mixin approach, the fields are created on the given table rather than linked through a foreign key. This might be handy for situations where we know we are going to need the same fields on a given table - such as the TrackingFields - and want to avoid code duplication. Granted, this might reduce some of the flexibility that a generic relation would afford us, but perhaps also reduce some of the complexity.
If you have other thoughts or suggestions regarding this, please feel free to share. I'm eager to learn of better approaches. Thank you again,
Joe
Thanks Eduardo... core_interfaces is a copy-paste error. I've made the correction. Thank you for pointing that out.
ReplyDeleteAlthough this is a good example of using mixins, I wouldn't call this an example of aspect-orientated programming.
ReplyDeleteThanks cody-somerville. Perhaps I was off on my naming. I'll adjust the title name.
ReplyDeleteI was trying to figure out the best way to do this (see http://stackoverflow.com/questions/6428075/is-it-ok-to-use-multiple-inheritance-with-django-abstract-models) , whether my Mixins needed to inherit from models.Model or not, and so forth. This is a good anwser.
ReplyDeleteName and slug are very generic too, you could put them in a mixin...
ReplyDeleteNice way of expressing your ideas with us.thanks for sharing with us and please add more information's.
ReplyDeleteJAVA Training in Chennai
JAVA Course in Chennai
Digital Marketing Course in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai
JAVA training in Annanagar
Java courses in chennai anna nagar
As always your articles do inspire me. Every single detail you have posted was great. ExcelR Pune Digital Marketing Course
ReplyDeletethank you for providing so useful information.keep it up
ReplyDeletePython Training in chennai | Python Classes in Chennai
Awesome post Python Training in Chennai
ReplyDeleteKeep up the good work , I read few posts on this web site and I conceive that your blog is very interesting
ReplyDeletevé máy bay từ singapore về hà nội vietjet
chuyến bay thương mại từ canada về việt nam
ve may bay tu Duc ve Viet Nam
Vé máy bay giá rẻ tu itali ve Viet Nam
Bảng giá vé máy bay Vietjet Air tu Ha Lan ve Viet Nam
Tra ve may bay gia re tu New Zealand ve Viet Nam
you are making for that security numerous pleasant factors here that I to hand your article various innovation. Your points of view are concurring inside the middle of my own personal for the most extent. this is allowable substance in your perusers. Facebook Account Hacker Pro
ReplyDeletefor certain i'm absolutely in all honesty later this article and that I on a very basic level pulse broadcast that this flyer is totally conceivable and truely educational article.i will make obvious for look at your weblog more. You made an unadulterated lightening in any event can't designate dissipate to regardless bewilderment, what by and large the fresh out of the plastic new part? !!!!!!thanks. Birthday Wish For Sister
ReplyDeleteThank you for providing this information. Visit us to get Web Development Company In India at Affordable prices.
ReplyDelete