[Answered ]-Django multiple databases and "cannot execute in a read-only transaction" error

0👍

It looks like there is no way to do this without sprinkling the .using('default') around in code.

👤Gady

2👍

Have you tried the following? This was copied out of the Django docs page:

import random

class MasterSlaveRouter(object):
    def db_for_read(self, model, **hints):
        """
        Reads go to a randomly-chosen slave.
        """
        return random.choice(['slave1', 'slave2'])

    def db_for_write(self, model, **hints):
        """
        Writes always go to master.
        """
        return 'master'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the master/slave pool.
        """
        db_list = ('master', 'slave1', 'slave2')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, model):
        """
        All non-auth models end up in this pool.
        """
        return True

Finally, in the settings file, we add the following (substituting path.to. with the actual python path to the module(s) where the routers are defined):

DATABASE_ROUTERS = ['path.to.AuthRouter', 'path.to.MasterSlaveRouter']

0👍

Got this too and posted a ticket for django developers.
Here it is: https://code.djangoproject.com/ticket/32965

There was an answer. There is django documentation saying .delete() and .update() use same database (allways readonly!) that was used to retrieve the object. So this is not a bug, but future because they say it documented.

So there is two ways to solve this problem:

  1. Add using(‘xxxx’) before every .delete() and .update()
  2. Replace .delete() by .clear() and .update() by .set()

Leave a comment