我試過使用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})
熊布朗,你的意思是國家= form.ModelChoiceField(查詢集= ListOfCountries .objects.all()),因爲,確切的國家不顯示?我試圖用這種方式取代國家。現在我看到國家名單,但這個名單不依賴大陸名單。如果我以歐洲大陸的形式選擇大陸1,那麼國家領域將向我展示所有的價值觀,而不僅僅是大陸1。對不起,我的英文不好 – delka
我可以使用Google環聊進行聊天嗎?我不使用Skype。你的電子郵箱是什麼? – delka
向你發送要求 – delka