router.py 1.11 KB
Newer Older
Ben Waters's avatar
Ben Waters committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class GeoRouter(object):
    """
    A router to control all database operations on models in the
    geo application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read geo models go to location.
        """
        if model._meta.app_label == 'geo':
            return 'location'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write geo models go to location.
        """
        if model._meta.app_label == 'geo':
            return 'location'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the geo app is involved.
        """
        if obj1._meta.app_label == 'geo' or \
           obj2._meta.app_label == 'geo':
           return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the geo app only appears in the 'location'
        database.
        """
        if db == 'location':
            return model._meta.app_label == 'geo'
        elif model._meta.app_label == 'geo':
            return False
        return None