Ticket #13640: 13640_ducktyping.diff

File 13640_ducktyping.diff, 1.9 KB (added by Tobias McNulty, 14 years ago)

alternate patch that uses duck typing to determine if value is an expression object

  • django/db/models/sql/query.py

    diff -r ad6737af409f django/db/models/sql/query.py
    a b  
    1414from django.db import connections, DEFAULT_DB_ALIAS
    1515from django.db.models import signals
    1616from django.db.models.fields import FieldDoesNotExist
     17from django.db.models.expressions import ExpressionNode
    1718from django.db.models.query_utils import select_related_descend, InvalidQuery
    1819from django.db.models.sql import aggregates as base_aggregates_module
    1920from django.db.models.sql.constants import *
     
    10021003            value = True
    10031004        elif callable(value):
    10041005            value = value()
    1005         elif hasattr(value, 'evaluate'):
     1006        elif hasattr(value, 'prepare') and hasattr(value, 'evaluate')\
     1007             and callable(value.prepare) and callable(value.evaluate):
    10061008            # If value is a query expression, evaluate it
    10071009            value = SQLEvaluator(value, self)
    10081010            having_clause = value.contains_aggregate
  • tests/regressiontests/model_regress/tests.py

    diff -r ad6737af409f tests/regressiontests/model_regress/tests.py
    a b  
    1 from models import Worker
     1from models import Worker, Department
    22from django.test import TestCase
    33
    44class RelatedModelOrderedLookupTest(TestCase):
     
    1414
    1515    def test_related_lte_lookup(self):
    1616        Worker.objects.filter(department__lte=0)
     17
     18
     19class EvaluateMethodTest(TestCase):
     20    """
     21    Regression test for #13640: cannot filter by objects with 'evaluate' attr
     22    """
     23
     24    def test_model_with_evaluate_method(self):
     25        """
     26        Ensures that you can filter by objects that have an 'evaluate' attr
     27        """
     28        dept = Department.objects.create(pk=1, name='abc')
     29        dept.evaluate = 'abc'
     30        Worker.objects.filter(department=dept)
Back to Top