Prechádzať zdrojové kódy

Add `StreamField.get_db_prep_value()` to delegate serialisation of `JSONField` lookup values (#9693)

Fixes #9692
Sage Abdullah 2 rokov pred
rodič
commit
573107a3b6
3 zmenil súbory, kde vykonal 12 pridanie a 0 odobranie
  1. 1 0
      CHANGELOG.txt
  2. 1 0
      docs/releases/4.2.md
  3. 10 0
      wagtail/fields.py

+ 1 - 0
CHANGELOG.txt

@@ -34,6 +34,7 @@ Changelog
  * Fix: Move DateField, DateTimeField, TimeField comment buttons to be right next to the fields (Theresa Okoro)
  * Fix: Support text resizing in workflow steps cards (Ivy Jeptoo)
  * Fix: Ignore images added via fixtures when using `WAGTAILIMAGES_FEATURE_DETECTION_ENABLED` to avoid errors for images that do not exist (Aman Pandey)
+ * Fix: Restore ability to perform JSONField query operations against StreamField when running against the Django 4.2 development branch (Sage Abdullah)
  * Docs: Add custom permissions section to permissions documentation page (Dan Hayden)
  * Docs: Add documentation for how to get started with contributing translations for the Wagtail admin (Ogunbanjo Oluwadamilare)
  * Docs: Officially recommend `fnm` over `nvm` in development documentation (LB (Ben) Johnston)

+ 1 - 0
docs/releases/4.2.md

@@ -46,6 +46,7 @@ depth: 1
  * Move DateField, DateTimeField, TimeField comment buttons to be right next to the fields (Theresa Okoro)
  * Support text resizing in workflow steps cards (Ivy Jeptoo)
  * Ignore images added via fixtures when using `WAGTAILIMAGES_FEATURE_DETECTION_ENABLED` to avoid errors for images that do not exist (Aman Pandey)
+ * Restore ability to perform JSONField query operations against StreamField when running against the Django 4.2 development branch (Sage Abdullah)
 
 ### Documentation
 

+ 10 - 0
wagtail/fields.py

@@ -212,8 +212,18 @@ class StreamField(models.Field):
             )
         else:
             # When querying with JSONField features, the rhs might not be a StreamValue.
+            # Note: when Django 4.2 is the minimum supported version, this can be removed
+            # as the serialisation is handled in get_db_prep_value instead.
             return self.json_field.get_prep_value(value)
 
+    def get_db_prep_value(self, value, connection, prepared=False):
+        if self.use_json_field and not isinstance(value, StreamValue):
+            # When querying with JSONField features, the rhs might not be a StreamValue.
+            # As of Django 4.2, JSONField value serialisation is handled in
+            # get_db_prep_value instead of get_prep_value.
+            return self.json_field.get_db_prep_value(value, connection, prepared)
+        return super().get_db_prep_value(value, connection, prepared)
+
     def from_db_value(self, value, expression, connection):
         if self.use_json_field and isinstance(expression, KeyTransform):
             # This could happen when using JSONField key transforms,