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.


我不完全确定你在问什么,但几年前David Cramer写了一个名为Django-identitymapper的图书馆 - 可以满足这个法案吗?


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.

链接地址: http://www.djcxy.com/p/60204.html

上一篇: 使用Django ORM处理大量的大型记录

下一篇: Django ORM和工作单元