diff --git a/example/app/test_msf.py b/example/app/test_msf.py index 3c73978..4f04d78 100644 --- a/example/app/test_msf.py +++ b/example/app/test_msf.py @@ -116,7 +116,17 @@ def test_flatchoices(self): self.assertEqual(Book._meta.get_field('published_in').flatchoices, list(PROVINCES + STATES)) def test_named_groups(self): - self.assertEqual(Book._meta.published_in.choices, PROVINCES_AND_STATES) + # We can't use a single self.assertEqual here, because model subchoices may be lists or tuples + # Iterate through the parent choices + for book_choices, province_or_state_choice in zip(Book._meta.get_field('published_in').choices, PROVINCES_AND_STATES): + parent_book_choice, *book_subchoices = book_choices + parent_pors_choice, *pors_subchoices = province_or_state_choice + # Check the parent keys + self.assertEqual(parent_book_choice, parent_pors_choice) + # Iterate through all of the subchoices + for book_subchoice, pors_subchoice in zip(book_subchoices, pors_subchoices): + # The model subchoices might be tuples, so make sure to convert both to lists + self.assertEqual(list(book_subchoice), list(pors_subchoice)) def test_named_groups_form(self): form_class = modelform_factory(Book, fields=('published_in',)) diff --git a/example/example/settings.py b/example/example/settings.py index 9c2754d..74290c4 100644 --- a/example/example/settings.py +++ b/example/example/settings.py @@ -40,6 +40,8 @@ } } +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + # Hosts/domain names that are valid for this site; required if DEBUG is False # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts ALLOWED_HOSTS = ['localhost'] diff --git a/multiselectfield/db/fields.py b/multiselectfield/db/fields.py index 902cc38..e888047 100644 --- a/multiselectfield/db/fields.py +++ b/multiselectfield/db/fields.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this programe. If not, see . +from django import VERSION from django.db import models from django.utils.text import capfirst from django.core import exceptions @@ -54,14 +55,17 @@ def __init__(self, *args, **kwargs): self.max_choices = kwargs.pop('max_choices', None) super(MultiSelectField, self).__init__(*args, **kwargs) self.max_length = get_max_length(self.choices, self.max_length) - self.validators[0] = MaxValueMultiFieldValidator(self.max_length) + self.validators.append(MaxValueMultiFieldValidator(self.max_length)) if self.min_choices is not None: self.validators.append(MinChoicesValidator(self.min_choices)) if self.max_choices is not None: self.validators.append(MaxChoicesValidator(self.max_choices)) def _get_flatchoices(self): - flat_choices = super(MultiSelectField, self)._get_flatchoices() + if VERSION >= (5,): + flat_choices = super(MultiSelectField, self).flatchoices + else: + flat_choices = super(MultiSelectField, self)._get_flatchoices() class MSFFlatchoices(list): # Used to trick django.contrib.admin.utils.display_for_field into