[英]get foreign key from model
I want to get pk
from model. 我想从模型中获取
pk
。
model.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='' )
views.py views.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 )
When I want to get ins.codecateg
I get categorie
object. 当我想获取
ins.codecateg
我得到了categorie
对象。
如果你想要的是pk
ins.codecateg.pk
In 'produit/modal_prode.html' you are sending Produit
object, so ins
is the object, and when you do ins.codecateg
it will get the categorie
object associated with it, but if you wnat to get the categorie
pk you would have to 在“produit / modal_prode.html”您要发送
Produit
对象,所以ins
是对象,而当你ins.codecateg
它将得到categorie
与它相关联的对象,但如果你wnat得到categorie
PK你就必须
<h4>{{ ins.codecateg.pk }}</h4>
in html. 在html中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.