根据Django中的Integerfield值进行下拉列表
我想显示nombre_des_emplacments integerfield
在rayons模型中作为下拉菜单中的下拉菜单,例如,输入的数字为44,我希望下拉菜单显示1和44之间的数字作为选择的选择,在添加引用后选择并保存一个数字后,必须从选择中取出,而在添加新参考时不再出现,EXP:如果在下一次下拉菜单时选择20将显示所有数字从1到44,除了20以外
,因此您知道nombre_des_emplacments integerfield
也可以依赖于Rayons.nom
从同一模型中的Rayons.nom
下拉菜单,
因此在添加参考时,我可以进入Emplacment字段,我选择人造丝,并取决于我选择的第二个下拉菜单将显示与Rayons的相关数字,
这是代码:型号
:py
class Rayons(models.Model):
Nom = models.CharField(max_length=100, blank=False, null=True)
Nombre_des_Emplacments = models.IntegerField()
def __str__(self):
return self.Nom
class References(models.Model):
Ref = models.CharField(max_length=255, blank=False, null=True)
Designation = models.CharField(max_length=100, blank=False, null=True)
Indice = models.CharField(max_length=100, blank=False, null=True)
Emplacment = models.ForeignKey(Rayons, blank=False, null=True, on_delete=models.CASCADE)
Client = models.ForeignKey(Client, blank=False, null=True, on_delete=models.CASCADE)
Statut = models.BooleanField(default=True)
def __str__(self):
return self.Ref
forms.py
class RayonsCreateform(forms.ModelForm):
class Meta:
model = Rayons
fields = ['Nom', 'Nombre_des_Emplacments']
def clean_Nom(self):
Nom = self.cleaned_data.get('Nom')
if not Nom:
raise forms.ValidationError('Ce champs est obligatoire')
for instance in Rayons.objects.all():
if instance.Nom == Nom:
raise forms.ValidationError(str(Nom) + ' existe déja dans la liste ')
return Nom
viewss.py viewss.py views。 PY HTML代码页list_references.html
def Ajouter_Rayons(request):
form = RayonsCreateform(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, 'Ajouté avec succès')
return redirect('/list_rayons')
context = {
"form": form,
"title": "Ajouter des Rayons",
}
return render(request, "Ajouter_Rayons.html", context)
def list_rayons(request):
header = 'Liste des rayons'
queryset = Rayons.objects.all()
form = RayonsSearchForm(request.POST or None)
context = {
"header": header,
"queryset": queryset,
"form": form,
}
if request.method == 'POST':
queryset = Rayons.objects.filter(Nom__icontains=form['Nom'].value(),
)
context = {
"form": form,
"header": header,
"queryset": queryset,
}
return render(request, "list_rayons.html", context)
添加添加的
<main role="main" class="container">
<div class="jumbotron">
<h1>{{ title }}</h1>
<form method='POST' action=''>{% csrf_token %}
{{form|crispy}}
<input type="submit" value='Save'/>
</form>
</div>
</main>
I want to display the Nombre_des_Emplacments IntegerField
in the Rayons model as a dropdown menu in the References Model below, for example the number inputted is 44, I want for the dropdown menu to show the numbers between 1 and 44 as choices to select from, and after a number is selected and saved after adding the References, it has to be taken out from the choices and not show up again when adding a new Reference, Exp: if 20 is selected next time the dropdown menu will show all the numbers from 1 to 44 except 20
Just so you know the Nombre_des_Emplacments IntegerField
is also suppossed to be dependent on Rayons.Nom
from the same Model which will also be a dropdown menu
So when adding a reference and I get to the Emplacment field, I select the Rayon and depending on what I selected the second dropdown menu will show the correlated numbers with that Rayons
Here's the code:
models:py
class Rayons(models.Model):
Nom = models.CharField(max_length=100, blank=False, null=True)
Nombre_des_Emplacments = models.IntegerField()
def __str__(self):
return self.Nom
class References(models.Model):
Ref = models.CharField(max_length=255, blank=False, null=True)
Designation = models.CharField(max_length=100, blank=False, null=True)
Indice = models.CharField(max_length=100, blank=False, null=True)
Emplacment = models.ForeignKey(Rayons, blank=False, null=True, on_delete=models.CASCADE)
Client = models.ForeignKey(Client, blank=False, null=True, on_delete=models.CASCADE)
Statut = models.BooleanField(default=True)
def __str__(self):
return self.Ref
forms.py
class RayonsCreateform(forms.ModelForm):
class Meta:
model = Rayons
fields = ['Nom', 'Nombre_des_Emplacments']
def clean_Nom(self):
Nom = self.cleaned_data.get('Nom')
if not Nom:
raise forms.ValidationError('Ce champs est obligatoire')
for instance in Rayons.objects.all():
if instance.Nom == Nom:
raise forms.ValidationError(str(Nom) + ' existe déja dans la liste ')
return Nom
views.py
def Ajouter_Rayons(request):
form = RayonsCreateform(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, 'Ajouté avec succès')
return redirect('/list_rayons')
context = {
"form": form,
"title": "Ajouter des Rayons",
}
return render(request, "Ajouter_Rayons.html", context)
def list_rayons(request):
header = 'Liste des rayons'
queryset = Rayons.objects.all()
form = RayonsSearchForm(request.POST or None)
context = {
"header": header,
"queryset": queryset,
"form": form,
}
if request.method == 'POST':
queryset = Rayons.objects.filter(Nom__icontains=form['Nom'].value(),
)
context = {
"form": form,
"header": header,
"queryset": queryset,
}
return render(request, "list_rayons.html", context)
HTML code of the adding of refrences page list_references.html
<main role="main" class="container">
<div class="jumbotron">
<h1>{{ title }}</h1>
<form method='POST' action=''>{% csrf_token %}
{{form|crispy}}
<input type="submit" value='Save'/>
</form>
</div>
</main>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论