Skip to content

Commit

Permalink
Django 5+ compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
blag committed May 20, 2024
1 parent 4d290af commit 422fc71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 11 additions & 1 deletion example/app/test_msf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',))
Expand Down
2 changes: 2 additions & 0 deletions example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
8 changes: 6 additions & 2 deletions multiselectfield/db/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this programe. If not, see <http://www.gnu.org/licenses/>.

from django import VERSION
from django.db import models
from django.utils.text import capfirst
from django.core import exceptions
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 422fc71

Please sign in to comment.