我需要在頁面中顯示候選人各自平均值的列表,候選人計算平均值的得分在另一個模型(評估)中,此模型接收包含該評估註釋的屬性得分即候選人收到並與候選人有外鍵,所以可能有幾個「評估」與同一候選人,我必須顯示所有候選人的各自平均數,但「候選人」模板無法訪問筆記,因爲它們被存儲在模板「評估」中,所以我認爲要做到這一點,就是將一個queryset的候選對象與候選對象進行比較,並根據當前結果計算平均值,在「Evaluation.objects.all()」中,然後在將候選人和他的平均數傳遞給一個放置在數組中的字典中,到目前爲止這麼好,它完美地工作,但是我需要當點擊候選人用戶將重定向(名稱和其他信息),但要使用「PK」我想要queryset,而不是某些字典?那麼,我的問題是什麼,我也在「評估」模板中嘗試過queryset,但是我無法從「評估」模板本身中訪問「Evaluation.objects.all()」以進行「評分」的收集並獲取平均而言,我真的不知道該在這裏做什麼,你有什麼建議?是否可以通過queryset訪問另一個模型的屬性?
models.py
from django.db import models
from jsonfield import JSONField
from site_.settings import MEDIA_ROOT
from django.core.validators import MaxValueValidator
class Criterion(models.Model):
label = models.CharField(max_length=100)
def __str__(self):
return self.label
class Candidate(models.Model):
name = models.CharField(max_length=100)
e_mail = models.EmailField(max_length=100, default = '')
github = models.URLField(default = '')
linkedin = models.URLField(max_length=100, default = '')
cover_letter = models.TextField(default = '')
higher_education = models.BooleanField(default = False)
docfile = models.FileField(upload_to='/home/douglas/Documentos/Django/my-second-blog/site_/media', null=True, blank=True)
def __str__(self):
return self.name
class Evaluation(models.Model):
candidate = models.ForeignKey(Candidate)
criterion = models.ForeignKey(Criterion, default='')
score = models.PositiveIntegerField(default = 0, validators=[MaxValueValidator(10)])
appraiser = models.ForeignKey('auth.User')
def __str__(self):
return str(self.candidate)
#model de teste
class Teste(models.Model):
nome = models.CharField(max_length=10)
def __str__(self):
return str(self.nome)
views.py
from django.shortcuts import render, get_object_or_404
from .models import Candidate, Criterion, Evaluation
from django import forms
from .forms import CandForm
from .forms import EvalForm
from .forms import TestForm
from django.shortcuts import redirect
from django.db import IntegrityError
import re
def canditate_list(request):
candidates = Candidate.objects.all()
evaluation = Evaluation.objects.all()
context = {
'candidates': candidates,
'evaluation': evaluation,
}
return render(request, 'app/candidate_list.html',context)
def candidate_detail(request, pk):
candidate = get_object_or_404(Candidate, pk=pk)
c_name = candidate.name #pega o nome (string) do candidato
c1 = Evaluation.objects.all() #guarda tds Evaluation na variavel
scores = [] #declara a array que vai receber as notas
for c in c1:
cand = str(c.candidate) #guarda o nome do candidato do Evaluation atual
if cand == c_name: #confere se o Evaluation atual corresponde ao candidate atual(pk)
scores += [c.score]
soma = 0 #variavel que guardara a soma declarada
for s in scores:
soma += s #faz a soma dos scores
average = 0
if len(scores) > 0:
average = soma/len(scores) #tira a média
context = {
'candidate': candidate,
'average': average,
}
return render(request, 'app/candidate_detail.html', context)
HTML
{% load staticfiles %}
{% load mathfilters %}
<!DOCTYPE html>
<html>
<head>
<title>Lista de candidatos</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link href="https://fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="{% static 'css/app.css' %}" rel="stylesheet">
</head>
<body>
<h1 class="h1t">Lista de Candidatos</h1>
{% for l in candidates %}
<p class="tr"><a href="{% url 'candidate_detail' l.pk %}">{{l}}</a></p>
{% endfor %}
</body>
</html>
但是,你建議我訪問平均名稱?正如我所說的,當我嘗試「average.candidate」時,需要在模板中顯示名稱和分數:「AttributeError:'float'object has no attribute'candidate'」 –