Opened 8 years ago

Closed 7 years ago

#14484 closed (worksforme)

database router ignored on Manager.get()

Reported by: qMax <qwiglydee@…> Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Keywords: sprintdec2010, multiple databases, router, manager
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


attempts to use default database, if called before
if .all is called first, .get uses proper database

router is trivial:

class DBRouter(object):
  def get_db(self, model):
    return model.db_name

  def db_for_read(self, model, **hints):
    return model.db_name

  def db_for_write(self, model, **hints):
    return model.db_name

  def allow_relation(self, obj1, obj2, **hints):
    return False

  def allow_syncdb(self, db, model):
    return False

Change History (2)

comment:1 Changed 8 years ago by qMax <qwiglydee@…>

not manager behaviour, but actually a queryset.
calling objects.all() is not sufficient, of course.
either len() or iterator should be called befor using .get()

comment:2 Changed 7 years ago by aweakley

Keywords: sprintdec2010 added
Resolution: worksforme
Status: newclosed

I can't repeat the behaviour described using a router as described above, and the following model code:

class Category(models.Model):
    title = models.CharField(max_length=127)
    db_name = 'other'

    def __unicode__(self):
        return self.title

DATABASE_ROUTERS = ['example.routers.DBRouter',]


In [1]: from example.models import Category

In [2]: Category.objects.get(id=1)

Out[2]: <Category: inside other database>

In [3]: Category.objects.all().get(id=1)

Out[3]: <Category: inside other database>

In [4]: Category.objects.all().db

Out[4]: 'other'

Note: See TracTickets for help on using tickets.
Back to Top