Answer the question
In order to leave comments, you need to log in
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
Add to model
class Meta:
managed = False
def save(self, *args, **kwargs):
raise ValueError('Read-only database')
def delete(self, *args, **kwargs):
raise ValueError('Read-only database')
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 questionAsk a Question
731 491 924 answers to any question