django-multiselectfield
django-multiselectfield copied to clipboard
fix(fields): append validator instead of pushing it with index error
Hello, added compatible with Django > 4
Hi, I also came to the same conclusion with this error. Don't see a reason why that value would need to be added exclusively at the zero index.
why this PR is not approved yet??
why this PR is not approved yet??
Hi, I will use for my projects fixed https://github.com/alpden550/django-multiselectfield as temporary
Related to #134
I think your code does not cover completly the need. If max_length is set, then the MaxLengthValidator
is added twice. One time by CharField
init and one time by MultiSelectField
init.
It is not too much of a worry to double check but not very useful either.
I would rather do this:
if self.max_length is None:
self.max_length = get_max_length(self.choices, self.max_length)
self.validators.append(MaxValueMultiFieldValidator(self.max_length))
else:
self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
Related to #134
I think your code does not cover completly the need. If max_length is set, then the
MaxLengthValidator
is added twice. One time byCharField
init and one time byMultiSelectField
init.It is not too much of a worry to double check but not very useful either.
I would rather do this:
if self.max_length is None: self.max_length = get_max_length(self.choices, self.max_length) self.validators.append(MaxValueMultiFieldValidator(self.max_length)) else: self.validators[0] = MaxValueMultiFieldValidator(self.max_length)
Dajngo 4 all ok, nothing doubled:
[ <django.core.validators.MaxLengthValidator object at 0xffffb4d450f0>, <multiselectfield.validators.MaxValueMultiFieldValidator object at 0xffffb4d44f70> ]
Although, we can wonder why there is a max_length
parameter at all. The max_length
should be inferred from the choices
as get_max_length
does. Indeed if we set the max_length
to 3 for example but our choices
is MY_CHOICES2 = ((1, 'Item title 1'), (2, 'Item title 2'), (3, 'Item title 3'))
, the necessary length is 5. It means that we cannot take all the choices together.
Admitting that this feature makes sense, you are checking twice : MaxValueMultiFieldValidator is a MaxLengthValidator !!
class MaxValueMultiFieldValidator(validators.MaxLengthValidator):
code = 'max_multifield_value'
def clean(self, x):
return len(','.join(x))
And you can run
for validator in my_field.validators:
print(validator.limit_value)
To see that they are duplicates.
See other PR
Fixed in #148.