#35972: Custom lookup example raises TypeError when used on a JSONField
-------------------------------+--------------------------------------
     Reporter:  Jacob Walls    |                    Owner:  (none)
         Type:  Bug            |                   Status:  closed
    Component:  Documentation  |                  Version:  dev
     Severity:  Normal         |               Resolution:  worksforme
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

 * resolution:   => worksforme
 * status:  new => closed
 * summary:  Custom lookup example raises TypeError => Custom lookup example
     raises TypeError when used on a JSONField

Comment:

 Testing against main with postgres 17 I don't get the TypeError but I get
 `django.db.utils.DataError: invalid input syntax for type json` (but I
 think that's expected as the documented example is not for JSON fields)

 So far I found no issues with the documented example

 This is what I have:
 {{{#!diff
 --- a/tests/custom_lookups/models.py
 +++ b/tests/custom_lookups/models.py
 @@ -18,3 +18,10 @@ class Article(models.Model):

  class MySQLUnixTimestamp(models.Model):
      timestamp = models.PositiveIntegerField()
 +
 +
 +class JSONModel(models.Model):
 +    value = models.JSONField()
 +
 +    class Meta:
 +        required_db_features = {"supports_json_field"}
 diff --git a/tests/custom_lookups/tests.py b/tests/custom_lookups/tests.py
 index 2f4ea0a9a0..9a7d86f8f9 100644
 --- a/tests/custom_lookups/tests.py
 +++ b/tests/custom_lookups/tests.py
 @@ -6,11 +6,11 @@ from django.core.exceptions import FieldError
  from django.db import connection, models
  from django.db.models.fields.related_lookups import RelatedGreaterThan
  from django.db.models.lookups import EndsWith, StartsWith
 -from django.test import SimpleTestCase, TestCase, override_settings
 +from django.test import SimpleTestCase, TestCase, override_settings,
 skipUnlessDBFeature
  from django.test.utils import register_lookup
  from django.utils import timezone

 -from .models import Article, Author, MySQLUnixTimestamp
 +from .models import Article, Author, MySQLUnixTimestamp, JSONModel


  class Div3Lookup(models.Lookup):
 @@ -249,6 +249,23 @@ class LookupTests(TestCase):
              self.assertSequenceEqual(qs1, [a1])
              self.assertSequenceEqual(qs2, [a1])

 +    @skipUnlessDBFeature("supports_json_field")
 +    def test_custom_lookup_json_field(self):
 +        class NotEqual(models.Lookup):
 +            lookup_name = "ne"
 +
 +            def as_sql(self, compiler, connection):
 +                lhs, lhs_params = self.process_lhs(compiler, connection)
 +                rhs, rhs_params = self.process_rhs(compiler, connection)
 +                params = lhs_params + rhs_params
 +                return "%s <> %s" % (lhs, rhs), params
 +
 +        json_model_instance = JSONModel.objects.create(value={"test":
 "a"})
 +
 +        with (register_lookup(models.JSONField, NotEqual)):
 +            qs = JSONModel.objects.filter(value__ne="somevalue")
 +            self.assertSequenceEqual(qs, [json_model_instance])
 +
      def test_custom_exact_lookup_none_rhs(self):
          """
          __exact=None is transformed to __isnull=True if a custom lookup
 class
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35972#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070193962049de-9c600dd4-a5cc-4e89-9401-e08e1cc08038-000000%40eu-central-1.amazonses.com.

Reply via email to