我想在 Django 中使用模型构建一个多级下拉层次结构。并且信息应该是动态更新而不重新加载页面。我知道我应该使用 Ajax,但是是否可以从以下模型生成模板:
class Pays(models.Model):
nom_pays=models.CharField(max_length=45)
def__str__(self):
return self.nom_pays
class Province(models.Model):
nom_province=models.CharField(max_length=100)
pays=models.ForeignKey(Pays,on_delete=models.CASCADE)
def__str__(self):
return self.nom_province
class Ville(models.Model):
nom_ville=models.CharField(max_length=100)
province=models.ForeignKey(Province,on_delete=models.CASCADE)
def__str__(self):
return self.nom_ville
class Commune(models.Model):
nom_commune=models.CharField(max_length=100)
ville=models.ForeignKey(Ville,on_delete=models.CASCADE)
def__str__(self):
return self.nom_commune
是否可以从 django 模型表单生成该模型,或者我应该使用 HTML 创建手动表单?
最佳答案
首先,你必须知道,我们不使用模型表格,因为它是唯一的做事方式。目的是赢得时间。所以手动完成所有这些人员并不是不可能的。就我个人而言,当我遇到这种情况时我总是手动构建表单,但只填充层次结构的第一个或最高模型,对于其他模型,我将使用 Ajax。例如:
def myController(request):
#To have the list of all you pays
pays=Pays.objects.all()
#Here you pass your pays object in your form template by a dictionnary
return render(request,"template/your_form_template.html",{"your_dic_variable":pays})
然后在您的模板中:
<select name="myChoice" id="choice">
{% for pays in your_dic_variable %}
<option value={{ pays.id }}>{{ pays.nom }}</option>
{% endfor %}
</select>
....
如果您有其他选择,可以调用
onChange()
发送 Ajax 请求以动态更新以下下拉列表的方法。正如您所说,如果您知道使用 Ajax,您将拥有如下内容:$(function(){
$('#choice')change(function()
{
//you can send ajax somewhere here
});
});
或者,另一个想法是,想象一下有 3 个这样的模型:
class FirstModel(models.model):
field_m1=models.CharField(max_length=20)
class SecondModel(models.model):
field_m2=models.CharField(max_length=20)
first_model=models.ForeignKey(FirstModel,on_delete=models.CASCADE)
class ThirdModel(models.model):
field_m3=models.CharField(max_length=20)
field2=models.CharField(max_length=20)
field3=models.CharField(max_length=20)
second_model=models.ForeignKey(SecondModel,on_delete=models.CASCADE)
如
ThirdModel
依赖于前 2 个模型的字段是 second_model
,您可以使用模型表单只有 3 个字段:fields_m3
, field2
和 field3
.它们将自动生成:class ThirdModelForm(froms.ModelForm):
class Meta:
model=ThirdModel
fields=['fields_m3','field2','field3']
在你的模板中,你会这样做:
<form>
{{ csrf_token }}
<!-- Data form first model,but you must have a controller send send the data -->
<select id="choice">
{% for f_model in your_dic_variable %}
<option value={{ f.id }}>{{ f.column }}</option>
{% endfor %}
</select>
<!-- second model will be populated with ajax form the choice1 id -->
<select id="choice2">
{% for f_model in your_dic_variable %}
<option value={{ f.id }}>{{ f.column }}</option>
{% endfor %}
</select>
<!-- second dropdown will be populated with ajax form the choice1 id -->
<select id="choice2">
<!-- data here will come from ajax according to the first model id value -->
</select>
<!-- third dropdown will be populated with ajax form the choice2 id,and it is the second_model field of ThirdModel data -->
<select id="choice2">
<!-- data here will come from ajax according to the second model id value -->
</select>
{{ ourform.as_p }}
<button type='submit'>Record</button>
</form>
我也做过,而且效果很好
关于python - 我想使用 django 模型表单生成 4 级下拉表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662358/