django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

Doesn't work on a Inline Admin Page

Open vs06 opened this issue 8 years ago • 15 comments
trafficstars

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 master branch 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_selects app.
  • [x] I have reduced the issue to the simplest possible case.
  • [x] I have included all relevant sections of models.py, forms.py, and views.py with problems.
  • [x] I have used GitHub Flavored Markdown to style all of my posted code.

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

vs06 avatar Jun 27 '17 17:06 vs06

What's the JSON that's erroring out?

blag avatar Jun 30 '17 04:06 blag

+1 is there some solutions?

trottomv avatar Jul 12 '18 17:07 trottomv

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

manelclos avatar Aug 18 '20 04:08 manelclos

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.

sahinakkaya avatar Apr 19 '21 20:04 sahinakkaya

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

manelclos avatar Apr 20 '21 08:04 manelclos

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

sahinakkaya avatar Apr 20 '21 09:04 sahinakkaya

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.

manelclos avatar Apr 20 '21 09:04 manelclos

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.

sahinakkaya avatar Apr 20 '21 09:04 sahinakkaya

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.

manelclos avatar Apr 20 '21 09:04 manelclos

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 avatar Apr 20 '21 10:04 sahinakkaya

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.

sahinakkaya avatar Apr 20 '21 10:04 sahinakkaya

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

manelclos avatar Apr 20 '21 10:04 manelclos

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

sahinakkaya avatar Apr 20 '21 11:04 sahinakkaya

How did you fix it?

pedrohschmidt avatar Apr 23 '22 02:04 pedrohschmidt

AFAIK, integer Primary Keys are still required

manelclos avatar Apr 25 '22 12:04 manelclos