django-smart-selects
django-smart-selects copied to clipboard
Doesn't work on a Inline Admin Page
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
- Use Django Smart Selects on a Inline Admin Page.
Actual behavior
I use Django Smart Selects to dynamically choose a document based on its document type. It work fine on an Admin Page, but I have another Page with an Inline Admin and there I've got this error:
?_changelist_filters=o%3D5.2.3%26p%3D2:1 Uncaught SyntaxError: Unexpected number in JSON at position 1
at JSON.parse (<anonymous>)
at initItem (bindfields.js:8)
at HTMLSelectElement.<anonymous> (bindfields.js:25)
at Function.each (jquery.min.js:2)
at HTMLDocument.<anonymous> (bindfields.js:24)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at Function.ready (jquery.min.js:2)
at HTMLDocument.J (jquery.min.js:2)
initItem @ bindfields.js:8
(anonymous) @ bindfields.js:25
each @ jquery.min.js:2
(anonymous) @ bindfields.js:24
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
J @ jquery.min.js:2
MyClass model:
class MyClass(BaseModel):
idcliente = models.IntegerField(default=102, editable=False)
(...)
idtipodoc = models.ForeignKey('Tipodoccliente', db_column='idtipodoc', related_name='+')
nrdoc = ChainedForeignKey(
Cliente,
chained_field="idtipodoc",
chained_model_field="idtipodoc",
show_all=False,
auto_choose=True,
db_column='nrdoc', related_name='+'
)
class Cliente(BaseModel):
idtipodoc = models.ForeignKey('Tipodoccliente', db_column='idtipodoc', verbose_name='Tipo do Documento', related_name='+')
nrdoc = models.CharField(primary_key=True, unique=True, max_length=15, verbose_name='Numero do Documento', validators=[numeric])
nome = models.CharField(max_length=100, verbose_name='Nome')
(...)
MyClass admin inline:
class MyClasslInline(admin.TabularInline):
model = MyClass
fields = ['idcliente', (...), 'nrdoc']
max_num = 1
What's the JSON that's erroring out?
+1 is there some solutions?
@vs06 there are several inlines in the test-app that are working correctly. We need more details on how to reproduce, ideally using the test-app. And also the JSON response would help here.
To reproduce this, you just need to save the model and try to change it again. I have asked a question on StackOverflow. I don't know if it's relevant but I see a very similar error message on the console.
@Asocia yes please, paste any JS error you get. Are you using the latest version?
Here is the error message:
Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
initItem http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:8
<anonymous> http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:36
each jQuery
<anonymous> http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:35
jQuery 4
i
fireWith
ready
J
bindfields.js:8:26
initItem http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:8
<anonymous> http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:36
each jQuery
<anonymous> http://127.0.0.1:8000/static/smart-selects/admin/js/bindfields.js:35
jQuery 4
i
fireWith
ready
J
Here is the output of pip freeze | grep smart:
django-smart-selects==1.5.9
Great, now if you could only check the "Network" tab when this happens, to see the contents of the response, as it seems there is something wrong there.
Nothing seems to wrong to me. Can you tell exactly which information you need?
Edit: I think I found something. I will share it soon.
As per your Stack Overflow description: "... when I change faculty from 1 to 2 dropdown for the programs are not updated ..."
Clean your network tab so you see no requests. Change Faculty from 1 to 2. At this point you should see a request in the network tab. We need to know what the content of this request is.
I think I found something. I will share it soon.
The syntax highlighting in the response was broken after some point like this:
So I thought there are some characters that is causing the problem. However, when I copy and format those lines, everything looks normal for me:
<div id="div_id_faculty" class="form-group">
<label for="id_faculty" class=" requiredField">
Faculty<span class="asteriskField">*</span> </label>
<div class="">
<select name="faculty" class="select form-control" required id="id_faculty">
<option value="">---------</option>
<option value="IN" selected>IN - İnşaat Fakültesi</option>
<option value="MM">MM - Mimarlık Fakültesi</option>
<option value="MK">MK - Makina Fakültesi</option>
<option value="EE">EE - Elektrik - Elektronik Fakültesi</option>
<option value="MD">MD - Maden Fakültesi</option>
<option value="KM">KM - Kimya - Metalurji Fakültesi</option>
<option value="IS">IS - İşletme Fakültesi</option>
<option value="GD">GD - Gemi İnşaatı ve Deniz Bilimleri Fakültesi</option>
<option value="FE">FE - Fen - Edebiyat Fakültesi</option>
<option value="UU">UU - Uçak ve Uzay Bilimleri Fakültesi</option>
<option value="KO">KO - Türk Musikisi Devlet Konservatuarı</option>
<option value="DZ">DZ - Denizcilik Fakültesi</option>
<option value="TK">TK - Tekstil Teknolojileri ve Tasarımı Fakültesi</option>
<option value="BB">BB - Bilgisayar ve Bilişim Fakültesi</option>
<option value="SN">SN - Uluslararası Ortak Lisans Programları</option>
<option value="KK">KK - İTÜ Kuzey Kıbrıs</option>
</select>
</div>
</div>
<div id="div_id_program" class="form-group">
<label for="id_program" class=" requiredField">
Program<span class="asteriskField">*</span> </label>
<div class="">
<select name="program" class="chainedselect form-control chained-fk" required id="id_program" data-chainfield="faculty" data-url="/chaining/filter/sis/Program/faculty/students/Student/program" data-value="BLGE" data-auto_choose="false" data-empty_label="--------" name="program">
<option value="">--------</option>
<option value="BLG">BB faculty's BLG program.</option>
<option value="BLGE" selected>BB faculty's BLGE program.</option>
<option value="YZVE">BB faculty's YZVE program.</option>
</select>
</div>
</div>
Clean your network tab so you see no requests. Change Faculty from 1 to 2. At this point you should see a request in the network tab.
No request happens when I do this.
@Asocia after looking at the html you pasted and noticed that you are not using integers for your Primary Keys, which is currently unsupported by smart-selects.
I got it. It would be nice if this was documented. Anyways, thanks for your time.
How did you fix it?
AFAIK, integer Primary Keys are still required