summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2019-10-31 20:33:16 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-11-05 11:48:44 +0100
commit47379d027ba2786403969367ec9c721936a823f8 (patch)
treec2aec2c7500c8910e2eec386697a31b65f2daeff
parentdc60597eb643814e54fbb0f7dadfe68c634e52fa (diff)
Fixed #30095 -- Fixed system check for RangeField/ArrayField.choices with lists and tuples.
-rw-r--r--django/contrib/postgres/fields/array.py4
-rw-r--r--django/contrib/postgres/fields/ranges.py4
-rw-r--r--tests/postgres_tests/test_array.py14
-rw-r--r--tests/postgres_tests/test_ranges.py14
4 files changed, 35 insertions, 1 deletions
diff --git a/django/contrib/postgres/fields/array.py b/django/contrib/postgres/fields/array.py
index f8ce7911ef..5f30ed1ab1 100644
--- a/django/contrib/postgres/fields/array.py
+++ b/django/contrib/postgres/fields/array.py
@@ -46,6 +46,10 @@ class ArrayField(CheckFieldDefaultMixin, Field):
self.__dict__['model'] = model
self.base_field.model = model
+ @classmethod
+ def _choices_is_value(cls, value):
+ return isinstance(value, (list, tuple)) or super()._choices_is_value(value)
+
def check(self, **kwargs):
errors = super().check(**kwargs)
if self.base_field.remote_field:
diff --git a/django/contrib/postgres/fields/ranges.py b/django/contrib/postgres/fields/ranges.py
index 953d02ac65..c0d985afa3 100644
--- a/django/contrib/postgres/fields/ranges.py
+++ b/django/contrib/postgres/fields/ranges.py
@@ -60,6 +60,10 @@ class RangeField(models.Field):
self.__dict__['model'] = model
self.base_field.model = model
+ @classmethod
+ def _choices_is_value(cls, value):
+ return isinstance(value, (list, tuple)) or super()._choices_is_value(value)
+
def get_prep_value(self, value):
if value is None:
return None
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py
index 379a1e9bba..7b7793f6c1 100644
--- a/tests/postgres_tests/test_array.py
+++ b/tests/postgres_tests/test_array.py
@@ -592,6 +592,20 @@ class TestChecks(PostgreSQLSimpleTestCase):
self.assertEqual(errors[0].id, 'postgres.E001')
self.assertIn('max_length', errors[0].msg)
+ def test_choices_tuple_list(self):
+ class MyModel(PostgreSQLModel):
+ field = ArrayField(
+ models.CharField(max_length=16),
+ choices=[
+ [
+ 'Media',
+ [(['vinyl', 'cd'], 'Audio'), (('vhs', 'dvd'), 'Video')],
+ ],
+ (['mp3', 'mp4'], 'Digital'),
+ ],
+ )
+ self.assertEqual(MyModel._meta.get_field('field').check(), [])
+
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific tests")
class TestMigrations(TransactionTestCase):
diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py
index 3702221107..b68f22112f 100644
--- a/tests/postgres_tests/test_ranges.py
+++ b/tests/postgres_tests/test_ranges.py
@@ -10,7 +10,7 @@ from django.test import override_settings
from django.utils import timezone
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
-from .models import RangeLookupsModel, RangesModel
+from .models import PostgreSQLModel, RangeLookupsModel, RangesModel
try:
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
@@ -413,6 +413,18 @@ class TestSerialization(PostgreSQLSimpleTestCase):
self.assertEqual(new_instance.ints, NumericRange(10, None))
+class TestChecks(PostgreSQLSimpleTestCase):
+ def test_choices_tuple_list(self):
+ class Model(PostgreSQLModel):
+ field = pg_fields.IntegerRangeField(
+ choices=[
+ ['1-50', [((1, 25), '1-25'), ([26, 50], '26-50')]],
+ ((51, 100), '51-100'),
+ ],
+ )
+ self.assertEqual(Model._meta.get_field('field').check(), [])
+
+
class TestValidators(PostgreSQLSimpleTestCase):
def test_max(self):