skip to navigation
skip to content

django-reverse-unique 1.0

A ReverseUnique field implementation for Django

A ReverseUnique model field implementation for Django

The ReverseUnique field can be used to access single model instances from the reverse side of ForeignKey. Essentially, ReverseUnique can be used to generate OneToOneField like behaviour in the reverse direction of a normal ForeignKey. The idea is to add an unique filter condition when traversing the foreign key to reverse direction.

To be able to use reverse unique, you will need a unique constraint for the reverse side or otherwise know that only one instance on the reverse side can match.


It is always nice to see actual use cases. We will model employees with time dependent salaries in this example. This use case could be modelled as:

class Employee(models.Model):
    name = models.TextField()

class EmployeeSalary(models.Model):
    employee = models.ForeignKey(Employee, related_name='employee_salaries')
    salary = models.IntegerField()
    valid_from = models.DateField()
    valid_until = models.DateField(null=True)

It is possible to save data like “Anssi has salary of 10€ from 2000-1-1 to 2010-1-1, and salary of 11€ from 2010-1-1 to infinity (modelled as None in the models).

Unfortunately when using these models it isn’t trivial to just fetch the employee and his salary to display to the user. It would be possible to do so by looping through all salaries of an employee and checking which of the EmployeeSalaries is currently in effect. However, this approach has a couple of drawbacks:

  • It doesn’t perform well in list views
  • Most of all It is impossible to execute queries that refer the employee’s current salary. For example, getting the top 10 best paid employees or the average salary of employees is impossible to achieve in single query.

Django-reverse-unique to the rescue! Lets change the Employee model to:

from datetime import datetime
class Employee(models.Model):
    name = models.TextField()
    current_salary = models.ReverseUnique(
        filter=Q( &
               (Q(valid_until__isnull=True) | Q(

Now we can simply issue a query like:




What did happen there? We added a ReverseUnique field. This field is the reverse of the EmployeeSalary.employee foreign key with an additional restriction that the relation must be valid at the moment the query is executed. The first “EmployeeSalary” argument refers to the EmployeeSalary model (we have to use string as the EmployeeSalary model is defined after the Employee model). The filter argument is a Q-object which can refer to the fields of the remote model.

Another common problem for Django applications is how to store model translations. The storage problem can be solved with django-reverse-unique. Here is a complete example for that use case:

from django.db import models
from reverse_unique import ReverseUnique
from django.utils.translation import get_language, activate

class Article(models.Model):
    active_translation = ReverseUnique("ArticleTranslation",

class ArticleTranslation(models.Model):
    article = models.ForeignKey(Article)
    lang = models.CharField(max_length=2)
    title = models.CharField(max_length=100)
    body = models.TextField()

    class Meta:
        unique_together = ('article', 'lang')

objs = Article.objects.filter(
# Generated query is
#    select article.*, article_translation.*
#      from article
#      join article_translation on article_translation.article_id =
#                               and article_translation.lang = 'fi'
# If you activate "en" instead, the lang is changed.
# Now you can access objs[0].active_translation without generating more
# queries.

Similarly one could fetch current active reservation for a hotel room etc.


The requirement for ReverseUnique is Django 1.6+. You will need to place the reverse_unique directory in Python path, then just use it like done in above example. The tests (reverse_unique/ contain a couple more examples. Easiest way to install is:

pip install -e git://


You’ll need to have a supported version of Django installed. Go to testproject directory and run:

python test reverse_unique
File Type Py Version Uploaded on Size
django-reverse-unique-1.0.tar.gz (md5, pgp) Source 2015-09-04 5KB
django_reverse_unique-1.0-py2.py3-none-any.whl (md5, pgp) Python Wheel py2.py3 2015-09-04 8KB