Creating Custom Managers For Repetitive Filters

September 30th, 2009

Today I was trying to do something in Django and I’ve noticed that I’m using lots of filter in my views. And most of the filters were same, but for different purposes. Then I felt like DRY principle is screaming at me. So I started to think that maybe there is a way to prevent these repetitions. And like all other problems in the world, the solution was just in front of my eyes :)

So let’s say that we’ve a model class that represents Employees in the company which includes even retired ones. To reach the all employees, you would use:


Employee.objects.all()

And if you want to work with retired employees:

Employee.objects.filter(status=Employee.RETIRED)

This would give you the all retired employees. If you’re working with this object list frequently, you might want to introduce a custom manager.

Custom Managers

Before going into the custom managers, I’d like to first explain where this Employee.objects comes from. objects attribute is an instance of a special class called Manager (django.db.models.Manager). The basic idea is to provide common db operations via this attribute, e.g all(), filter(), etc.. You can find the full documentation from here. If you don’t specify any custom managers, Django automatically adds this default manager to your model class.

Custom managers on the other hand is the extended version of this manager class. And creating a custom manager is fairly easy:

class MyModel(models.Model):
    myfield = models.CharField(max_length=50)

object_provider = models.Manager()

So above model’s object_provider attribute would act exactly same as the default manager since they’re same. Please check below usages:

'''
  Below usages would provide same results.
'''
MyModel.objects.all()
MyModel.object_provider.all()

So how to customize this manager then? Again, very easy. Just extend the django.db.models.Manager class. So let’s look at the following code:

class RetiredEmployeeManager(models.Manager):
   def get_query_set(self):
      return super(RetiredEmployeeManager, self).get_query_set().filter(status=Employee.RETIRED)

From this point, the only thing you need to do is defining this manager as your manager.

class Employee(models.Model):
    name = models.CharField(max_length=50)

objects = models.Manager()
retired_ones = RetiredEmployeeManager()

And finally, you can use your new manager in your views:

Employee.retired_ones.all();

Hope it helps you to clean up some duplicated code!

Django

No Comments

Generating Lorem Ipsums in Django Templates

September 19th, 2009

When we first attempt create our web applications, most of us use a common printing phrase which starts with Lorem Ipsum words in our templates. Today while I was reading something in the Django documentation, and surprisingly I came across with this very useful module. Honestly, I didn’t even know Django has such a thing. After seeing this module, I again thought about how much I love Django :)

Anyways, the idea is generating this placeholder words in your template by using a template tag. Here is the usage:

<!-- 
    make sure you're loading webdesign module 
    by writing {% load webdesign %} 
-->
<div id='my-text-area'>
   {% lorem %}
   <!--
       This will be replaced with "Lorem ipsum dolor 
       sit amet, consectetur adipiscing elit..."
   -->
</div>

<input type='text name='name' value='{% lorem 2 w random %}' /> <!-- This will be replaced with 2 random latin words -->

<div id='description'> {% lorem 5 p %} <!-- This will be replaced with 5 paragraphs of "Lorem Ipsum.." text --> </div>

Isn’t Django awesome :) It has everything you need!

Django

No Comments

Learning Site Packages Folder Under OSX

September 13th, 2009

Today I was browsing couple django extensions to check if there is any new useful stuff around. I saw couple questions in those WiKis/forums about site-packages folder under OSX. For those who don’t know which site-packages folder is active in your system, you must consider how your system is configured. Especially if you’re using MacPorts or any tool like that, your default site-packages folder that is shipped with OS X might be different than what you’re presuming.

Anyways, the best way to learn what’s your active site-packages folder, you can quickly learn it by typing:

$ python -c \ 
"from distutils.sysconfig import get_python_lib;   \
 print get_python_lib()"

Or you can create a script like below and place it under your /usr/bin folder for further uses.

from distutils.sysconfig import get_python_lib;

print get_python_lib()

Django

No Comments