Giter VIP home page Giter VIP logo

django-reverse-unique's Introduction

django-reverse-unique

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 from a ForeignKey. You will need an unique filter condition on the reverse ForeignKey relation so that there is at most one instance on the reverse side that can match.

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.

The usage is simple:

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",
                                       filters=Q(lang=get_language))

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')

activate("fi")
objs = Article.objects.filter(
    active_translation__title__icontains="foo"
).select_related('active_translation')
# Generated query is something like:
#    select article.*, article_translation.*
#      from article
#      join article_translation on article_translation.article_id = 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.

# The active_translation lookup should work in all places where core
# fields can be used.

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

Installation

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/tests.py) contain a couple more examples. Easiest way to install is:

pip install -e git://github.com/akaariai/django-reverse-unique.git#egg=reverse_unique

Known issues

None currently. Note that django-reverse-unique is at alpha stage currently.

django-reverse-unique's People

Contributors

akaariai avatar charettes avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.