maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

What about django db routing? #9

Closed unaxfromsibiria closed 9 years ago

unaxfromsibiria commented 9 years ago

if has more one db connection, have to do like this:

from architect.orms.django.mixins import PartitionableMixin
from django.db import connections
from django.db.utils import ConnectionDoesNotExist

class RoutingPartitionableMixin(PartitionableMixin):

    def get_cursor(self):
        try:
            cursor = connections[self.PartitionableMeta.db_name].cursor()
        except AttributeError:
            cursor = super(RoutingPartitionableMixin, self).get_cursor()
        except ConnectionDoesNotExist as err:
            # need self.PartitionableMeta.db_name
            raise err

        return cursor

I would like something native implementation for this. Please, think about.

maxtepkeev commented 9 years ago

Great idea. I'll put it in my TODO list. Thanks.

unaxfromsibiria commented 9 years ago

after version 0.4.0 forced to replace cursor as:

try:
    SomeModelClass.architect.operation.cursor = connections['some_not_default_connection'].cursor()
except ConnectionDoesNotExist:
    # use default
    pass

I think good idea a give native opportunity for replacing feature classes, like this:

from architect.orms.django.features import PartitionFeature

class CustomPartitionFeature(PartitionFeature):
    def __init__(self, *args, **kwargs):
        super(PartitionFeature, self).__init__(*args, **kwargs)
        self.cursor = connections['some_not_default_connection'].cursor()

SomeModelClass.architect.set_feature_class('partition', CustomPartitionFeature)

This will be usefull for resolving other users needs, not only replacing cursor :)

maxtepkeev commented 9 years ago

Support for multiple database connections in Django and custom features are available in v0.5.0

unaxfromsibiria commented 9 years ago

thanks!