Opened 14 years ago

Closed 14 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: no UI/UX: no

Description

ModelInNondefaultDatabase.objects.get(key=val)
attempts to use default database, if called before
ModelInNondefaultDatabase.objects.all()
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 by qMax <qwiglydee@…>, 14 years ago

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 by aweakley, 14 years ago

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:

models.py

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

    def __unicode__(self):
        return self.title

settings.py

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

session:

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