django-smart-selects
django-smart-selects copied to clipboard
bindfields.js expects 'data-value' to be JSON but widgets.py doesn't always encode it
Checklist
Put an x in the bracket when you have completed each task, like this: [x]
- [ X] This issue is not about installing previous versions of django-smart-selects older than 1.2.8. I understand that previous versions are insecure and will not receive any support whatsoever.
- [ X] I have verified that that issue exists against the
masterbranch of django-smart-selects. - [ X] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [ X] I have debugged the issue to the
smart_selectsapp. - [ X] I have reduced the issue to the simplest possible case.
- [ X] I have included all relevant sections of
models.py,forms.py, andviews.pywith problems. - [X ] I have used GitHub Flavored Markdown to style all of my posted code.
Steps to reproduce
- Have a formset with 2 smart selects, load the page
Actual behavior
Uncaught SyntaxError: Unexpected token _ in JSON at position 2 at JSON.parse (<anonymous>) at initItem (bindfields.js:8) at HTMLSelectElement.<anonymous> (bindfields.js:25) at Function.each (jquery-2.2.4.js:365) at HTMLDocument.<anonymous> (bindfields.js:24) at fire (jquery-2.2.4.js:3187) at Object.fireWith [as resolveWith] (jquery-2.2.4.js:3317) at Function.ready (jquery-2.2.4.js:3536) at HTMLDocument.completed (jquery-2.2.4.js:3552)
Expected behavior
No Javascript error occurs.
Suggested Fix:
The problem is that in widgets.py, ChainedSelect.render line 138 sets:
attrs["data-value"] = "null" if value is None or value == "" else value
So when the string "foo" is decoded, it fails with JSON decode error above (should be '"foo"' (with quotes))
I believe the fix should be:
attrs["data-value"] = "null" if value is None else json.dumps(value)
Which is the same as line 244 in ChainedSelectMultiple
@davmlaw can you reproduce with latest master version?
Sorry, I use Django autocomplete light now, so can't easily test this. Feel free to close if you think it's fixed.