mirror of
https://github.com/django/django.git
synced 2025-08-28 14:34:27 +00:00
Fixed #28577 -- Added checks for ArrayField and JSONField to prevent mutable defaults.
This commit is contained in:
parent
5ceaf14686
commit
f6e1789654
8 changed files with 112 additions and 6 deletions
|
@ -4,7 +4,7 @@ import unittest
|
|||
import uuid
|
||||
|
||||
from django import forms
|
||||
from django.core import exceptions, serializers, validators
|
||||
from django.core import checks, exceptions, serializers, validators
|
||||
from django.core.exceptions import FieldError
|
||||
from django.core.management import call_command
|
||||
from django.db import IntegrityError, connection, models
|
||||
|
@ -424,6 +424,38 @@ class TestChecks(PostgreSQLTestCase):
|
|||
self.assertEqual(len(errors), 1)
|
||||
self.assertEqual(errors[0].id, 'postgres.E002')
|
||||
|
||||
def test_invalid_default(self):
|
||||
class MyModel(PostgreSQLModel):
|
||||
field = ArrayField(models.IntegerField(), default=[])
|
||||
|
||||
model = MyModel()
|
||||
self.assertEqual(model.check(), [
|
||||
checks.Warning(
|
||||
msg=(
|
||||
"ArrayField default should be a callable instead of an "
|
||||
"instance so that it's not shared between all field "
|
||||
"instances."
|
||||
),
|
||||
hint='Use a callable instead, e.g., use `list` instead of `[]`.',
|
||||
obj=MyModel._meta.get_field('field'),
|
||||
id='postgres.E003',
|
||||
)
|
||||
])
|
||||
|
||||
def test_valid_default(self):
|
||||
class MyModel(PostgreSQLModel):
|
||||
field = ArrayField(models.IntegerField(), default=list)
|
||||
|
||||
model = MyModel()
|
||||
self.assertEqual(model.check(), [])
|
||||
|
||||
def test_valid_default_none(self):
|
||||
class MyModel(PostgreSQLModel):
|
||||
field = ArrayField(models.IntegerField(), default=None)
|
||||
|
||||
model = MyModel()
|
||||
self.assertEqual(model.check(), [])
|
||||
|
||||
def test_nested_field_checks(self):
|
||||
"""
|
||||
Nested ArrayFields are permitted.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue