我想pk
从模型中获取。
模型.py
class Produit(models.Model):
ref=models.CharField(max_length=100, default='',primary_key=True)
marq=models.CharField(max_length=100, default='')
nomp=models.CharField(max_length=100, default='')
qte = models.IntegerField(default=0)
codecateg=models.ForeignKey(categorie, on_delete=models.CASCADE)
class categorie(models.Model):
codecateg=models.CharField(max_length=100, default='' , primary_key=True)
nomcat=models.CharField(max_length=30, default='' )
视图.py
def edit_prod(request , id = None):
ins = get_object_or_404(Produit,ref=id)
datedit = {'ins': ins}
return render(request, 'produit/modal_prode.html',datedit )
当我想得到时,ins.codecateg
我得到了categorie
对象。
在“produit/modal_prode.html”中,您正在发送Produit
对象,对象ins
也是如此,当您这样做时ins.codecateg
,它将获得categorie
与之关联的对象,但如果您想获得categorie
pk,则必须
<h4>{{ ins.codecateg.pk }}</h4>
在 html 中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句