V
V
Vladimir Kuts2016-11-23 14:51:49
Django
Vladimir Kuts, 2016-11-23 14:51:49

How to prevent django from creating unnecessary tables?

There are two bases in the project - one is 'default', the other is 'legacy', let's say.
'legacy' is created with inspectdb - data is read from it without any problem.
But, - the base is read-only - I only need to read data from there, any write operations are prohibited.
The problem is that with any migrations, django tries to create the 'django_migrations' table there, it fails, and the migration script fails. Because of what I can not do any migrations to the default database.
How to prevent django from doing this on a read-only base?
The router I use is something like this:

class LegacyRouter(object):

    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'legacy_app':
            return 'legacy'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'legacy_app':
            return False
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'legacy_app' or obj2._meta.app_label == 'legacy_app':
            return False
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == 'legacy_app':
            return False
        return None

    def allow_syncdb(self, db, model):
        if db == 'legacy' or model._meta.app_label == 'legacy_app':
             return False

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-11-23
@deliro

Add to model

class Meta:
    managed = False

Also, in order not to shoot myself in the foot, I made these stubs in the model:
def save(self, *args, **kwargs):
    raise ValueError('Read-only database')

def delete(self, *args, **kwargs):
    raise ValueError('Read-only database')

And in the manager:
class CardserverQuerySet(models.QuerySet):
    def create(self, *args, **kwargs):
        raise ValueError('Read-only database')

    def update(self, *args, **kwargs):
        raise ValueError('Read-only database')

    def delete(self, *args, **kwargs):
        raise ValueError('Read-only database')

    def bulk_create(self, *args, **kwargs):
        raise ValueError('Read-only database')

    def get_or_create(self, *args, **kwargs):
        raise ValueError('Read-only database')

    def update_or_create(self, *args, **kwargs):
        raise ValueError('Read-only database')


class CardserverManager(models.Manager):
    def get_queryset(self):
        return CardserverQuerySet(self.model, using='remote')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question