Fixed #30095 -- Fixed system check for RangeField/ArrayField.choices with lists and tuples.

This commit is contained in:
Hasan Ramezani 2019-10-31 20:33:16 +01:00 committed by Mariusz Felisiak
parent dc60597eb6
commit 47379d027b
4 changed files with 35 additions and 1 deletions

View File

@ -46,6 +46,10 @@ class ArrayField(CheckFieldDefaultMixin, Field):
self.__dict__['model'] = model self.__dict__['model'] = model
self.base_field.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): def check(self, **kwargs):
errors = super().check(**kwargs) errors = super().check(**kwargs)
if self.base_field.remote_field: if self.base_field.remote_field:

View File

@ -60,6 +60,10 @@ class RangeField(models.Field):
self.__dict__['model'] = model self.__dict__['model'] = model
self.base_field.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): def get_prep_value(self, value):
if value is None: if value is None:
return None return None

View File

@ -592,6 +592,20 @@ class TestChecks(PostgreSQLSimpleTestCase):
self.assertEqual(errors[0].id, 'postgres.E001') self.assertEqual(errors[0].id, 'postgres.E001')
self.assertIn('max_length', errors[0].msg) 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") @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific tests")
class TestMigrations(TransactionTestCase): class TestMigrations(TransactionTestCase):

View File

@ -10,7 +10,7 @@ from django.test import override_settings
from django.utils import timezone from django.utils import timezone
from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase
from .models import RangeLookupsModel, RangesModel from .models import PostgreSQLModel, RangeLookupsModel, RangesModel
try: try:
from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
@ -413,6 +413,18 @@ class TestSerialization(PostgreSQLSimpleTestCase):
self.assertEqual(new_instance.ints, NumericRange(10, None)) 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): class TestValidators(PostgreSQLSimpleTestCase):
def test_max(self): def test_max(self):