error : 'Processus' object has no attribute 'outil_set' - django
error : 'Processus' object has no attribute 'outil_set' - django
你好,我想在我的 'Processus' 中显示 'outils'
所以在我看来,我做了类似的事情:
def processus(request, pk_test):
processus = Processus.objects.get(id=pk_test)
outils = processus.outil_set.all()
total_outils = outils.count()
context={
'processus':processus,
'outils':outils,
'total_outils':total_outils,
}
但它不起作用它显示错误 'Processus' 对象没有属性 'outil_set'
这是我的models.py
class outil(ressource):
nom_outil = models.CharField(max_length=250)
proprieté = models.CharField(max_length=250)
technique_id = models.ForeignKey(technique, on_delete = models.CASCADE)
def __str__(self):
return self.nom_outil
class Processus(models.Model):
nom = models.CharField(max_length=250)
proprieté = models.CharField(max_length=250)
outils = models.ManyToManyField(outil)
def __str__(self):
return self.nom
关系的名称是 outils
,因此您可以使用 processus.outils.all()
:
访问它
def processus(request, pk_test):
processus = Processus.objects.get(id=pk_test)
outils = processus<b>.outils</b>.all()
total_outils = outils.count()
context={
'processus':processus,
'outils': outils,
'total_outils':total_outils,
}
# …
Note: It is often better to use get_object_or_404(…)
[Django-doc],
then to use .get(…)
[Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using
.get(…)
will result in a HTTP 500 Server Error.
Note: Models in Django are written in PerlCase, not snake_case,
so you might want to rename the model from outil
to Outil
.
你好,我想在我的 'Processus' 中显示 'outils' 所以在我看来,我做了类似的事情:
def processus(request, pk_test):
processus = Processus.objects.get(id=pk_test)
outils = processus.outil_set.all()
total_outils = outils.count()
context={
'processus':processus,
'outils':outils,
'total_outils':total_outils,
}
但它不起作用它显示错误 'Processus' 对象没有属性 'outil_set'
这是我的models.py
class outil(ressource):
nom_outil = models.CharField(max_length=250)
proprieté = models.CharField(max_length=250)
technique_id = models.ForeignKey(technique, on_delete = models.CASCADE)
def __str__(self):
return self.nom_outil
class Processus(models.Model):
nom = models.CharField(max_length=250)
proprieté = models.CharField(max_length=250)
outils = models.ManyToManyField(outil)
def __str__(self):
return self.nom
关系的名称是 outils
,因此您可以使用 processus.outils.all()
:
def processus(request, pk_test):
processus = Processus.objects.get(id=pk_test)
outils = processus<b>.outils</b>.all()
total_outils = outils.count()
context={
'processus':processus,
'outils': outils,
'total_outils':total_outils,
}
# …
Note: It is often better to use
get_object_or_404(…)
[Django-doc], then to use.get(…)
[Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, theget_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using.get(…)
will result in a HTTP 500 Server Error.
Note: Models in Django are written in PerlCase, not snake_case, so you might want to rename the model from
tooutil
Outil
.