我需要测试一些与我的模型和表单相关的东西,所以我使用 python3 manage.py shell 打开shell,然后我正确导入模型和表单并采用随机模型把它放在变量(c1)中所以我这样做: form = AvalForm(c1)
之后: form.is_valid()
当我第一次运行它时会返回错误:
AttributeError:'Avaliacao'对象没有属性'get'
但是当我再次运行.is_valid时,返回 True
然后我去了下一个通道......
我运行 form.fields 并正确返回表单中的所有字段,直到这里看起来都好......
但是当我尝试使用form.cleaned_data ['avaliador']获取指定字段时,我有:
> Traceback (most recent call last): File "<console>", line 1, in > <module> KeyError: 'avaliador'
但是当我输入 form.fields ...
时,该字段会出现好吧让代码:
models.py
from django.db import models
from jsonfield import JSONField
from site_.settings import MEDIA_ROOT
from django.core.validators import MaxValueValidator
class Criterio(models.Model):
label = models.CharField(max_length=100)
def __str__(self):
return self.label
class Candidato(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 = '')
Ensino_superior = models.BooleanField(default = False)
med = models.IntegerField(default = 0)
#talvez tenha que alterrar essa linha
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 Avaliacao(models.Model):
candidato = models.ForeignKey(Candidato)
criterio = models.ForeignKey(Criterio, default='')
nota = models.PositiveIntegerField(default = 0, validators=[MaxValueValidator(10)])
avaliador = models.ForeignKey('auth.User')
def __str__(self):
return str(self.candidato)
views.py
from django.shortcuts import render, get_object_or_404
from .models import Candidato, Criterio, Avaliacao
from django import forms
from .forms import CandForm
from .forms import AvalForm
from django.shortcuts import redirect
def canditato_list(request):
candidatos = Candidato.objects.all()
return render(request, 'app/candidato_list.html', {'candidatos': candidatos})
def candidato_detalhe(request, pk):
candidato = get_object_or_404(Candidato, pk=pk)
return render(request, 'app/candidato_detalhe.html', {'candidato': candidato})
def avaliar(request):
##################################################################################
aval = Avaliacao.objects.all() #
lista_cand = [] #
for a in aval: #nesse bloco eu ponho tds os avaliadores
lc = str(a.avaliador) #existentes em uma lista
lista_cand += [lc] #
##################################################################################
if request.method == "POST":
#############################################
form2 = AvalForm(request.POST)
#############################################
if form2.is_valid():
#############################################################################################
avaliador = form2.cleaned_data['avaliador'] ###ai aqui eu guardo o avaliador atual na variavel
avaliador = str(avaliador)
#############################################################################################
### aqui eu faço o teste
#############################################################################################
cont = 0
for l in lista_cand:
if avaliador == l:
cont += 1
#############################################################################################
post = form2.save(commit=False)
#avaliador = form2.fields['avaliador']
post.save()
#faz redirecionara para uma page que só tenha{'avaliador': avaliador} p/ testar a variavel avaliador
return redirect('canditato_list')
else:
form2 = AvalForm()
#avaliador = form2.fields['avaliador']
#teste
return render(request, 'app/avaliacao.html', {'criterios': form2,})
def cadastrar(request):
if request.method == "POST":
form = CandForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('candidato_detalhe', pk=post.pk)
else:
form = CandForm()
return render(request, 'app/cadastro.html', {'form': form})
ps:忽略所有评论和###### 之间的所有代码
forms.py
from .models import Candidato, Avaliacao, Teste
from django import forms
class CandForm(forms.ModelForm):
class Meta:
model = Candidato
fields = ('name', 'e_mail', 'github', 'linkedin', 'Ensino_superior','cover_letter')
class AvalForm(forms.ModelForm):
class Meta:
model = Avaliacao
fields = ('candidato','criterio','nota','avaliador')
#fields = ('v1','v2')
[编辑]
我正在建立一个为职位空缺注册候选人的系统,会有一个评估系统(这是'评估'模型),候选人每个评估者只能评估一次,我想做的是测试在保存模型(.save())之前,第一个标记('#####')之间的内容是'def evaluate(request):'之后不久,是一个循环,它将所有已经建立了它们的评估者将它们保存在列表中。在第二个标记之后,在'if form2.is_valid()之后:'我将当前评估者(转换为字符串)保存到变量中('evaluationator = form2.cleaned_data ['evaluationator'] - 'evaluator = str) “)。之后我进行测试,在这种情况下,将是另一个循环,将接收到转换为字符串的求值程序的变量与评估者列表中的每个求值程序(我将其命名为'list_cand',也是保存转换为字符串的数据,以便我可以进行比较),循环将通过此列表检查变量中的字符串是否在列表中的某个插槽中,如果是,我将发出警告或什么,否则它继续评估过程。在评论'###这里,我做测试'是这个测试的草稿。