2017-07-27 38 views
0

我試過使用django-smart-select。(ChainedForeignKey)。但模板不會顯示 的值無錯誤,但我看不到字段國家/地區中的值列表。 Country field picture: 也許我在ChainedForeignKey(),我不明白加錯了屬性=(請幫我解決它感謝 Models.py:Django-smart-selection:在'Country'字段中看不到值

from smart_selects.db_fields import ChainedForeignKey 
    class ListOfContinents(models.Model): 


id_Continent = models.AutoField(primary_key=True) 
     Continent = models.CharField(max_length=200,unique=True) 

     def __str__(self): 
      return '%s' % self.Continent 

    class ListOfCountries(models.Model): 

     id_Country = models.AutoField(primary_key=True) 
     Continent = models.ForeignKey(
      ListOfContinents, default=0, to_field='Continent',db_column='Continent') 
     Country=models.CharField(max_length=200,unique=True) 

     def __str__(self): 
      return '%s' % self.Country 



    class Location(models.Model): 


     Continent=models.ForeignKey(
      ListOfContinents,to_field='Continent', 
      db_column='Continent',verbose_name='Континент',null=True) 
     Country = ChainedForeignKey(ListOfCountries,chained_field="Continent", 
      chained_model_field="Continent", 
      show_all=False, 
      auto_choose=True, 
      sort=True, 
      verbose_name='Страна', 
      db_column='Country', 
      to_field='Country', 
      null=True) 

Forms.py:

class LocationForm (forms.ModelForm): 
    class Meta: 
     model = Location 
     fields = ('Continent','Country') 

Templates.py:

<form method="POST" class="post-form">{% csrf_token %} 
     {{ form_add.as_p }} 
     <button type="submit" class="save btn btn-default">Save</button> 
    </form> 

視圖:

def Loc(request): 
    if request.method == "POST": 
     form_add = LocationForm(request.POST) 
     if form_add.is_valid(): 

      Loc.save() 

      return redirect('Loc_edit_full') 
    else: 
     form_add = LocationForm() 
    return render(request, 'utest_base/Loc_add.html', {'form_add': form_add}) 

回答

0

試試吧,不要忘了進口ListOfContinents

class LocationForm (forms.ModelForm): 
    continent = forms.ModelChoiceField(queryset=ListOfContinents.objects.all()) 
    class Meta: 
     model = Location 
     fields = ('continent','Country') 
+0

熊布朗,你的意思是國家= form.ModelChoiceField(查詢集= ListOfCountries .objects.all()),因爲,確切的國家不顯示?我試圖用這種方式取代國家。現在我看到國家名單,但這個名單不依賴大陸名單。如果我以歐洲大​​陸的形式選擇大陸1,那麼國家領域將向我展示所有的價值觀,而不僅僅是大陸1。對不起,我的英文不好 – delka

+0

我可以使用Google環聊進行聊天嗎?我不使用Skype。你的電子郵箱是什麼? – delka

+0

向你發送要求 – delka

0

在這種情況下,需要添加腳本到模板

<form method="POST" class="post-form"> 
    {% csrf_token %} 
    {{ form_add.as_p }} 
    <button type="submit" class="save btn btn-default">Save</button> 
</form> 

<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script> 
<script src="{% static 'smart-selects/admin/js/bindfields.js' %}"></script> 
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script> 
+0

熊布朗,謝謝。另外重要的是要刪除to_field = '國家' 在國家= ChainedForeignKey(ListOfCountries,chained_field = 「大陸」, chained_model_field = 「大陸」, SHOW_ALL =假 auto_choose =真, 排序=真, verbose_name ='Страна ', db_column ='Country',to_field ='Country', - 需要刪除此 null = True) – delka