Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ORM & Unit of Work

Is there any easy way / library / external app to introduce Unit of Work concept to Django ORM? What approaches or techniques do you use to solve the problem of importing the same row twice in a complicated model setup without loosing all the modularity?

EDIT

Example

Consider the following examplatory situation - there is a model Location which has a relationship with itself called route through an additional model Route. Now let's say each Route has attributes called: entry_fare (the amount of money you need to pay to enter the route) and exit_fare (the amount of money you need to pay to exit the route on its other end). Now let's say you want to implement an action of augmenting the entry_fares and augmenting exit_fares. You also want to be able calculate the total sum of fares for a given city. You may want to perform a series of such actions inside a single transaction. Reads (comuting the sum) are much more frequent than writes (augmenting the fares).

In a naive implementation you would need to load a fresh set of models each time you want to calculate the sum - to ensure there is no stale data. Also augmenting would operate on a new set of models each time and would save instances immediately after the fare is augmented to ensure that further fetches from the database include the new fare values.

Remember, this is meant to serve as an example.

like image 383
julx Avatar asked Jun 05 '11 20:06

julx


2 Answers

I'm not entirely sure what you're asking, but a few years ago David Cramer wrote a library called Django-identitymapper - could that fit the bill?

like image 148
Daniel Roseman Avatar answered Sep 23 '22 15:09

Daniel Roseman


You can execute raw SQL, or manage the transactions. Managers also make it very easy to implement custom functionality and precise control over your models. You might want to consider using SQLAlchemy instead, it has built-in support for this.

Sounds like you're mostly interesting in caching models, django-cache-machine handles caching/invalidation using memcached.

like image 23
zeekay Avatar answered Sep 23 '22 15:09

zeekay