Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Creating a Mixin for Reusable Model Fields

I've got a few fields that I want to add to most every model in my project. For example, these fields are "tracking fields" such as a created date, an update date, and an "active" flag. I'm attempting to create a Mixin that I could add to each model class that would allow me to add these extra fields via multiple inheritance. However, when an object instance is created, it appears that my model fields that were added via the Mixin show up as methods of the object rather than database fields.

In [18]: Blog.objects.all()[0].created Out[18]: <django.db.models.fields.DateTimeField object at 0x10190efd0> 

Here is what my models look like:

from django.db import models  class Blog(models.Model, TrackingFieldMixin):     name = models.CharField(max_length=64)     type = models....   class TrackingFieldsMixin():      active = models.BooleanField(default=True,          help_text=_('Indicates whether or not this object has been deleted.'))     created = models.DateTimeField(auto_now_add=True)     modified = models.DateTimeField(auto_now=True)      class Meta:         abstract = True 

So this doesn't appear to work. Does anyone know how I am able to create a reusable mixin for common model fields similar to above? Is there a flaw in this approach?

Thanks for you help, Joe

Update: Note that some of my models that I plan to use the mixin in use the MPTT model, so I can't simply make my TrackingFieldMixin mixin the base class and inherit only from it.

class Post(MPTTModel, TrackingFieldMixin):     post_name = models....     post_type = models... 
like image 649
Joe J Avatar asked May 16 '11 07:05

Joe J


People also ask

What is model mixin in Django?

Model mixins are abstract classes that can be added as a parent class of a model. Python supports multiple inheritances, unlike other languages such as Java. Hence, you can list any number of parent classes for a model. Mixins ought to be orthogonal and easily composable.

Why are mixins used in Django?

A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used: You want to provide a lot of optional features for a class. You want to use one particular feature in a lot of different classes.

Can a mixin inherit from another mixin?

In JavaScript we can only inherit from a single object. There can be only one [[Prototype]] for an object. And a class may extend only one other class.

What is single object mixin Django?

SingleObjectMixin. Provides a mechanism for looking up an object associated with the current HTTP request. Methods and Attributes model. The model that this view will display data for. Specifying model = Foo is effectively the same as specifying queryset = Foo.


1 Answers

Abstract models still need to inherit from model.Model to work correctly:

class TrackingFieldsMixin(models.Model): 

Also instead of your active BooleanField I would add a deleted_on DateTimeField so you can record when the record was deleted. You can then just add properties on the instance to see if it's active:

@property def active(self):     return self.deleted_on is None 

and in queries and/or a custom manager:

Blog.objects.filter(deleted_on__isnull=True) 
like image 184
Sam Dolan Avatar answered Oct 11 '22 21:10

Sam Dolan