Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An identify mapper for the Django ORM

branch: master
1 comment

Switch to signals as they're more reliable, and ensure we always push…

… instances into the cache (loses savings on instance creation time, but ensures we dont maintain incorrect data)
latest commit 31ea23262a
David Cramer authored February 01, 2012
Octocat-spinner-32 idmapper
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 MANIFEST.in
Octocat-spinner-32 README.rst
Octocat-spinner-32 setup.py kill requirements since it messes with dev versions May 26, 2010
README.rst

This fork of django-idmapper fixes some bugs that prevented the idmapper from being used in many instances. In particular, the caching manager is now inherited by SharedMemoryManager subclasses, and it is used when Django uses an automatic manager (see http://docs.djangoproject.com/en/dev/topics/db/managers/#controlling-automatic-manager-types). This means access through foreign keys now uses identity mapping.

Tested with Django version 1.2 alpha 1 SVN-12375.

My modifications are usually accompanied by comments marked with "CL:".

Django Identity Mapper

A pluggable Django application which allows you to explicitally mark your models to use an identity mapping pattern. This will share instances of the same model in memory throughout your interpreter.

Please note, that deserialization (such as from the cache) will not use the identity mapper.

Usage

To use the shared memory model you simply need to inherit from it (instead of models.Model). This enable all queries (and relational queries) to this model to use the shared memory instance cache, effectively creating a single instance for each unique row (based on primary key) in the queryset.

For example, if you want to simply mark all of your models as a SharedMemoryModel, you might as well just import it as models.

from idmapper import models

class MyModel(models.SharedMemoryModel):
    name = models.CharField(...)

Because the system is isolated, you may mix and match SharedMemoryModels with regular Models. The module idmapper.models imports everything from django.db.models and only adds SharedMemoryModel, so you can simply replace your import of models from django.db.

from idmapper import models

class MyModel(models.SharedMemoryModel):
    name = models.CharField(...)
    fkey = models.ForeignKey('Other')

class Other(models.Model):
    name = models.CharField(...)

References

Original code and concept: http://code.djangoproject.com/ticket/17

Something went wrong with that request. Please try again.