Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError with Django 4.1 #131

Closed
symphoton opened this issue Aug 4, 2022 · 7 comments
Closed

IndexError with Django 4.1 #131

symphoton opened this issue Aug 4, 2022 · 7 comments

Comments

@symphoton
Copy link

  • Unfortunately this package breaks with Django 4.1.

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

  • Steps to reproduce:
    Use a MultiSelectField without explicitly specifying max_lenght attribute

  • Reason for failure is the following django commit:
    django/django@0ed2919

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

  • Proposed solution:
  1. Append MaxValueMultiFieldValidator in case len(self.validators)==0.
  2. Keep current logic if len(self.validators)>0 to stay compatible with older Django versions.
  • 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
@hmpf
Copy link

hmpf commented Oct 3, 2022

This also holds for Django 4.0.

@mortenthansen
Copy link

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

Thanks for the workaround.

@megges2000
Copy link

  • 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
Copy link

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
Copy link
Contributor

@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),
    )

ArhanChaudhary added a commit to ArhanChaudhary/TimeWeb that referenced this issue Feb 15, 2023
ddabble added a commit to MAKENTNU/web that referenced this issue Feb 27, 2023
* `SuccessURLAllowedHostsMixin` (used in `LoginView`) has been replaced by `RedirectURLMixin`
  (see https://docs.djangoproject.com/en/4.1/releases/4.1/#miscellaneous).
* Logging out via GET is deprecated in favor of POST (see https://docs.djangoproject.com/en/4.1/releases/4.1/#log-out-via-get),
  so added a logout form to the header which is used instead of the previous <a> button, and which appears when not using Dataporten for auth.
  `dataporten.views.Logout` can keep using GET, though it should ideally be replaced by POST as well.
* Added the `USES_DATAPORTEN_AUTH` custom setting for use in both `web/context_processors.py` and `web/urls.py`
* Made the `defer` dicts in `make_queue/forms.py` and `web/multilingual/widgets.py` use boolean values, now that it's supported by `django-js-asset`
  due to the changes in Django 4.1 (see matthiask/django-js-asset@3785c6c)
* Explicitly set `max_length` of `ReservationRule.start_days`, due to goinnn/django-multiselectfield#131
@violuke
Copy link

violuke commented May 13, 2023

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

chigby added a commit to newamericafoundation/newamerica-cms that referenced this issue Jan 12, 2024
* We have to declare `max_length` on `Survey.data_type` because of
goinnn/django-multiselectfield#131
* `url` has been removed and replaced by `re_path
* The migration was auto-generated, I'm not completely sure by what.
chigby added a commit to newamericafoundation/newamerica-cms that referenced this issue May 10, 2024
* We have to declare `max_length` on `Survey.data_type` because of
goinnn/django-multiselectfield#131
* `url` has been removed and replaced by `re_path`
* The migration was auto-generated, I'm not completely sure by what.
chigby added a commit to newamericafoundation/newamerica-cms that referenced this issue May 10, 2024
* We have to declare `max_length` on `Survey.data_type` because of
goinnn/django-multiselectfield#131
* `url` has been removed and replaced by `re_path`
* The migration was auto-generated, I'm not completely sure by what.
@blag
Copy link
Collaborator

blag commented May 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants