Skip to content

Conversation

@jieter
Copy link

@jieter jieter commented Feb 15, 2021

If the form value for a ModelSelectField for numeric foreign key is non-numeric, rendering the form
crashes with:

Field 'id' expected a number but got "13'[0]".

(full traceback)

This proposes ignoring non-numeric values for numeric fields. A small test case is included.

@codecov
Copy link

codecov bot commented Feb 15, 2021

Codecov Report

Merging #36 (6b21286) into master (e735be2) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@ Coverage Diff @@ ## master #36 +/- ## ======================================= Coverage 99.57% 99.57% ======================================= Files 7 7 Lines 233 237 +4 ======================================= + Hits 232 236 +4  Misses 1 1 
Impacted Files Coverage Δ
django_select2/forms.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e735be2...6b21286. Read the comment docs.

@jieter jieter force-pushed the feature/expecting-number-crash branch from 1c7aeeb to 08b0311 Compare February 15, 2021 15:19
…d(widget=ModelSelect2Widget(...)) If the form value for a ModelSelectField for numeric foreign key is non-numeric, rendering the form crashes with: > Field 'id' expected a number but got "13'[0]". This proposes ignoring non-numeric values for numeric fields
@jieter jieter force-pushed the feature/expecting-number-crash branch from 08b0311 to 6b21286 Compare February 15, 2021 15:21
@codingjoe codingjoe self-requested a review February 20, 2021 12:28
Copy link
Owner

@codingjoe codingjoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jieter,

A familiar face! Thanks for your contribution, and thanks for providing the stack trace. However, I am struggling to understand where this non-numeric values is coming from.
Maybe you could elaborate a bit more on your setup and this case.

Best,
Joe

@jieter
Copy link
Author

jieter commented Feb 20, 2021

Almost 3 years ago already!

We use the widget as part of a django-filter filterset. I'm not sure why this weird values get in the query string, but crash reporting shows lots of crashes related to this.

When I use the default widget for ModelSelectField and pass a non-numeric value, the form is considered invalid (which is different from what I propose here), and rendered as such. But it does not result in a crash.

In my opinion it is a reasonable expectation for this widget not to crash. Dealing with the origin of the weird values might become a little harder, but the UI should make clear to the user that no item is selected in the widget.

@codingjoe
Copy link
Owner

I see, hm… If this comes from a programmatic error I am hesitant to silence the error. To my knowledge a user can't pass a non-numeric value, only a developer can. I would agree to maybe provide a better exception message, to silence it, seems wrong. Besides, I just explained in another post, why cleaning value for a primary key in Django is almost impossible, since all different field types raise different errors.

@codingjoe codingjoe closed this Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants