Opened 11 years ago

Closed 11 years ago

#7095 closed (fixed)

Error with Many2Many filter() in models managers after queryset-refactor merge

Reported by: fcaprioli@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


first of all, thank you all for your excellent work; after the qs-rf branch merge, there is a small regression that cause a python error in during an update() if a custom Manager tries to .filter() thru a ManyToManyField in get_query_set().
This breaks, for example, CurrentSiteManager with a M2M 'site' field.

So, if you have a model like:

from django.db import models
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager

class Dummy(models.Model):
    dumb = models.TextField()
    site = models.ManyToManyField(Site)
    objects = CurrentSiteManager()

def dummy_view(request):
    t = Dummy.objects.create(dumb='abc') would give a TypeError: object of type 'Query' has no len().
At some point, a Query object get passed to make_atom rather than a list.
This is the traceback:

File "D:\Arturo\django\core\handlers\" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File "D:\Arturo\arturo5\magazine\" in article_add
File "D:\Arturo\django\db\models\" in save
  298.         self.save_base()
File "D:\Arturo\django\db\models\" in save_base
  338.                     manager.filter(pk=pk_val)._update(values)
File "D:\Arturo\django\db\models\" in _update
  299.         query.execute_sql(None)
File "D:\Arturo\django\db\models\sql\" in execute_sql
  112.         super(UpdateQuery, self).execute_sql(result_type)
File "D:\Arturo\django\db\models\sql\" in execute_sql
  1432.             sql, params = self.as_sql()
File "D:\Arturo\django\db\models\sql\" in as_sql
  136.         where, params = self.where.as_sql()
File "D:\Arturo\django\db\models\sql\" in as_sql
  61.                     sql, params = self.make_atom(child, qn)
File "D:\Arturo\django\db\models\sql\" in make_atom
  130.             return ('%s IN (%s)' % (field_sql, ', '.join(['%s'] * len(value))),

Exception Type: TypeError at /arturo/magazine/add/
Exception Value: object of type 'Query' has no len()

Change History (4)

comment:1 Changed 11 years ago by Malcolm Tredinnick

(In [7494]) Allow Query objects to be values in query filters. This already existed for
relations, but not for normal fields. The latter comes up naturally in some
update statements.

Refs #7095

comment:2 Changed 11 years ago by Malcolm Tredinnick

Triage Stage: UnreviewedAccepted

The above commit fixes the problem for SQLite and PostgreSQL. Unfortunately, it reveals another bug that affects MySQL users because of MySQL's non-standard UPDATE syntax rules. Working on that piece of the puzzle now.

comment:3 Changed 11 years ago by Malcolm Tredinnick

(In [7495]) Added a test to demonstrate the remaining problem in #7095.

Only fails for MySQL (because they've made some interesting syntax choices).
Refs #7095.

comment:4 Changed 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [7496]) Made some types of nested update queries very slightly more efficient at the
database level. Also worked around the fact that MySQL (and maybe other
backends we don't know about) cannot select from the table they're updating.

Fixed #7095.

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