Django

Code

Ticket #7235 (closed: invalid)

Opened 2 months ago

Last modified 2 weeks ago

filtering on an EmptyQuerySet raises an Exception

Reported by: anonymous Assigned to: nobody
Milestone: 1.0 Component: Database wrapper
Version: SVN Keywords: qsrf-cleanup query,emptyqueryset
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Trying to filter an instance of EmptyQuerySet? causes an Exception to be raised:

>>> from django.db.models.query import *
>>> q = EmptyQuerySet()
>>> q.filter(x=10)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 370, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/query.py", line 388, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/sql/query.py", line 1068, in add_q
    can_reuse=used_aliases)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/sql/query.py", line 954, in add_filter
    opts = self.get_meta()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/db/models/sql/query.py", line 127, in get_meta
    return self.model._meta
AttributeError: 'NoneType' object has no attribute '_meta'
>>> 

I would think that no matter what you pass in as arguments to filter(), it should always return EmptyQuerySet?.

Attachments

Change History

05/14/08 10:36:30 changed by john_scott

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I can confirm that the above error occurs with Django version 0.97-pre-SVN-7477

With Django version 0.97-pre-SVN-7476 I get the expected behavior:

>>> from django.db.models.query import EmptyQuerySet
>>> q = EmptyQuerySet()
>>> q.filter(x=10)
[]

06/10/08 10:01:41 changed by gav

  • keywords changed from query,emptyqueryset to qsrf-cleanup query,emptyqueryset.

06/10/08 17:26:05 changed by emulbreh

  • component changed from Core framework to Database wrapper.
  • stage changed from Unreviewed to Design decision needed.

Do QuerySets? without an associated Model make sense?

Solution proposals:

# Keep EmptyQuerySet?, require the model argument: nothing to do. # Keep EmptyQuerySet?, make the model argument optional: overwrite all methods that return a derived QuerySet? and return self (since QuerySets? are immutable). This would be uncomfortable to maintain. # Remove EmptyQuerySet?, add an is_empty flag to Query: given that Query is already 1.5kloc ... (allthough enabling something like query.mark_empty() in add_to_query() methods would be nice).

Additionally: If EmptyQuerySet? doesn't require the model argument, there might as well be a single instance. Otherwise one instance per model would do.

I'd give this a try, as long as the tempting third option isn't choosen (I'm not comfortable with the Query code).

06/16/08 12:10:45 changed by jacob

  • milestone set to 1.0.

06/26/08 07:21:19 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to invalid.

This isn't a valid problem, since creating an EmptyQuerySet directly is not something you ever do. It's an instance that is created (correctly) using the none() method on other querysets or model managers. Internal bits of code can sometimes create QuerySet classes and subclasses without immediately setting the model, which is why it's permitted, but this bug report is just a case of "if you use the code incorrectly, you get nonsense results." There is nothing in the documentation to suggest that what you tried here should work, whereas working with the result of a none() call does (or should) work correctly.


Add/Change #7235 (filtering on an EmptyQuerySet raises an Exception)




Change Properties
Action