goinnn / django-multiselectfield

A Multiple Choice model field
GNU Lesser General Public License v3.0
449 stars 208 forks source link

Support Django 5.0 #142

Closed aliceni81 closed 6 months ago

aliceni81 commented 11 months ago

The current version is incompatible with Django 5.0. There is no _get_flatchoices in django's CharField anymore, instead Django provides flatchoices directly. Therefore, the following code in db/fields.py should be removed.

def _get_flatchoices(self):
        flat_choices = super(MultiSelectField, self)._get_flatchoices()

        class MSFFlatchoices(list):
            # Used to trick django.contrib.admin.utils.display_for_field into
            # not treating the list of values as a dictionary key (which errors
            # out)
            def __bool__(self):
                return False
            __nonzero__ = __bool__
        return MSFFlatchoices(flat_choices)
flatchoices = property(_get_flatchoices)
ssherchan08 commented 11 months ago

I was also wondering why my data was not being fetched. Spent hours debugging. Thanks for this! It helped me save so much time.

symphoton commented 11 months ago

In my application, simply removing those lines breaks the display in the admin view. I recommend keeping the code, but replacing

flat_choices = super(MultiSelectField, self)._get_flatchoices()

by

flat_choices = super(MultiSelectField, self).flatchoices

or

try:
    flat_choices = super(MultiSelectField, self).flatchoices
except AttributeError:
    flat_choices = super(MultiSelectField, self)._get_flatchoices()

for backwards compatibility

sadra-allahyari commented 10 months ago

Big thanks, everyone! I was struggling to figure out how to fix it.