Code

Opened 4 years ago

Closed 4 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:

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

Attachments (0)

Change History (2)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

  • Keywords sprintdec2010, added
  • Resolution set to worksforme
  • Status changed from new to closed

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'

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.