dlamotte / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
0 stars 0 forks source link

`usage_for_queryset` and `with_all` don't play nice. #194

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Passing a queryset created by `with_all()` to `usage_for_queryset()` appears to 
kill the queryset 
somehow, although it is still a `django.db.models.query.QuerySet` object.

>>> from tagging.models import Tag
>>> job_set = Job.tagged.with_all('flash')
>>> type(job_set)
<class 'django.db.models.query.QuerySet'>
>>> job_set.count()
3
>>> len(Tag.objects.usage_for_queryset(job_set))
14
>>> type(job_set)
<class 'django.db.models.query.QuerySet'>
>>> job_set.count()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/path/to/django/db/models/query.py", line 329, in count
    return self.query.get_count()
  File "/path/to/django/db/models/sql/query.py", line 332, in get_count
    number = obj.get_aggregation()[None]
  File "/path/to/django/db/models/sql/query.py", line 304, in get_aggregation
    result = query.execute_sql(SINGLE)
  File "/path/to/django/db/models/sql/query.py", line 1980, in execute_sql
    cursor.execute(sql, params)
  File "/path/to/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
  File "/path/to/django/db/backends/sqlite3/base.py", line 190, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: no such column: credentials_job.id

I can work-around this by passing a copy of the Job queryset (using `all()`) to 
`usage_for_queryset`:

>>> job_set = Job.tagged.with_all('flash')
>>> len(Tag.objects.usage_for_queryset(job_set.all()))
14
>>> job_set.count()
3

Passing an ordinary queryset also appears to work:

>>> job_set = Job.objects.filter(pk=1)                
>>> len(Tag.objects.usage_for_queryset(job_set.all()))
5
>>> job_set.count()                                   
1

I'm not sure if the bug is in `with_all()` or `usage_for_queryset()`, but would 
be happy to try and 
fix (and contribute a patch) if someone can point me in the right direction.

A hack fix would be for `usage_for_queryset()` to make its own copy of the 
passed in queryset, 
though this would be treating the symptom and not the cause.

Original issue reported on code.google.com by real.hu...@mrmachine.net on 7 Apr 2009 at 8:24