goinnn / django-multiselectfield

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

IndexError with Django 4.1 #131

Closed symphoton closed 4 months ago

symphoton commented 2 years ago

The error message is as follows:

File "*****lib/python3.9/site-packages/multiselectfield/db/fields.py", line 72, in init self.validators[0] = MaxValueMultiFieldValidator(self.max_length) IndexError: list assignment index out of range

Obviously, django team realized that always adding a MaxValueFieldValidator if max_lenght is omitted is not a good idea for derived classes (like MultiSelectField) and

hmpf commented 1 year ago

This also holds for Django 4.0.

mortenthansen commented 1 year ago

I am seeing the same issue after an upgrade from django 4.0.7 to 4.1.2

Thanks for the workaround.

megges2000 commented 1 year ago
  • Potential workaround for users: explicitly specify max_length in MultiSelectField, this should lead to old behaviour even with Django 4.1 Drawback is that this disables the automatic max_length computation of this packages. Therefore be careful to choose max_lenght big enough

Thanks for that. I just use the get_max_length function for max_length, so the automatic max_length computation still works.

from multiselectfield.utils import get_max_length
from multiselectfield import MultiSelectField

class TestModel(models.Model):
    multifield = MultiSelectField(choices=MultiChoices.choices, blank=True, null=True, max_length=get_max_length(MultiChoices.choices, None))
PraveenChordia commented 1 year ago

Multi

  • Potential workaround for users: explicitly specify max_length in MultiSelectField, this should lead to old behaviour even with Django 4.1 Drawback is that this disables the automatic max_length computation of this packages. Therefore be careful to choose max_lenght big enough

Thanks for that. I just use the get_max_length function for max_length, so the automatic max_length computation still works.

from multiselectfield.utils import get_max_length
from multiselectfield import MultiSelectField

class TestModel(models.Model):
    multifield = MultiSelectField(choices=MultiChoices.choices, blank=True, null=True, max_length=get_max_length(MultiChoices.choices, None))

MultiChoices not defined.

mikemanger commented 1 year ago

@PraveenChordia Set choices to be a list of options. Usually you'd define a constant in the model class and reference it like so

from multiselectfield import MultiSelectField
from multiselectfield.utils import get_max_length

class TestModel(models.Model):
    MULTIFIELD_CHOICES = (
        ('read', 'Code'),
        ('you', 'Copy'),
    )
    multifield = MultiSelectField(
        choices=MULTIFIELD_CHOICES,
        max_length=get_max_length(MULTIFIELD_CHOICES, None),
    )
violuke commented 1 year ago

@goinnn thanks for the great package, but would it be possible to merge #132 so this can work on Django 4.0+?

blag commented 4 months ago

Django 4.1 support has already ended, but we still test against Django 4.2 and 5.0, and tests are passing. Closing, since I think we fixed this in #148 and elsewhere.