jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.11k stars 348 forks source link

Doesn't work on a Inline Admin Page #221

Open vs06 opened 7 years ago

vs06 commented 7 years ago

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

Steps to reproduce

  1. 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
blag commented 7 years ago

What's the JSON that's erroring out?

trottomv commented 6 years ago

+1 is there some solutions?

manelclos commented 3 years ago

@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.

sahinakkaya commented 3 years ago

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.

manelclos commented 3 years ago

@Asocia yes please, paste any JS error you get. Are you using the latest version?

sahinakkaya commented 3 years ago

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
manelclos commented 3 years ago

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.

sahinakkaya commented 3 years ago

image 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.

manelclos commented 3 years ago

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.

sahinakkaya commented 3 years ago

I think I found something. I will share it soon.

The syntax highlighting in the response was broken after some point like this: image 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&#x27;s BLG program.</option>
         <option value="BLGE" selected>BB faculty&#x27;s BLGE program.</option>
         <option value="YZVE">BB faculty&#x27;s YZVE program.</option>
      </select>
   </div>
</div>
sahinakkaya commented 3 years ago

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.

manelclos commented 3 years ago

@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.

sahinakkaya commented 3 years ago

I got it. It would be nice if this was documented. Anyways, thanks for your time.

pedrohschmidt commented 2 years ago

How did you fix it?

manelclos commented 2 years ago

AFAIK, integer Primary Keys are still required