1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #35381 -- Added JSONNull() expression.

Thanks Jacob Walls for the review.
This commit is contained in:
Clifford Gama
2025-08-07 17:26:15 +02:00
committed by Jacob Walls
parent ab108bf94d
commit adc25a9a66
7 changed files with 213 additions and 10 deletions

View File

@@ -403,6 +403,13 @@ class CustomJSONDecoder(json.JSONDecoder):
return dct
class JSONNullCustomEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, models.JSONNull):
return None
return super().default(o)
class JSONModel(models.Model):
value = models.JSONField()
@@ -422,6 +429,15 @@ class NullableJSONModel(models.Model):
required_db_features = {"supports_json_field"}
class JSONNullDefaultModel(models.Model):
value = models.JSONField(
db_default=models.JSONNull(), encoder=JSONNullCustomEncoder
)
class Meta:
required_db_features = {"supports_json_field"}
class RelatedJSONModel(models.Model):
value = models.JSONField()
json_model = models.ForeignKey(NullableJSONModel, models.CASCADE)