我尝试重新初始化我的数据库,我有这个错误:
django.db.utils.OperationalError:没有这样的表:app_evaluation
我没有任何带有此名称的变量,我尝试删除db.sqlite3和迁移文件夹的所有文件并运行migrate和makemigrations命令,但nothings无效
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)
average = models.IntegerField(default = 0)
#############################################################score = models.ForeignKey()
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, unique=True)
#s_candidate = models.CharField(max_length=100)
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)
class avarage(models.Model):
eva = Evaluation.objects.get()
view.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 django.shortcuts import redirect
def canditate_list(request):
candidates = Candidate.objects.all()
eva = Evaluation.objects.all()
eval_cand_list = [] #aqui guarda uma lista com os FK candidates convertidos p/ str
context = {
'candidates': candidates,
'eva': eva
}
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]
_sum = 0 #variavel que guardara a soma declarada
for s in scores:
_sum += s #faz a soma dos scores
average = _sum/len(scores) #tira a média
context = {
'candidate': candidate,
'average': average
}
return render(request, 'app/candidate_detail.html', context)
def evaluation(request):
if request.method == "POST":
form2 = EvalForm(request.POST)
if form2.is_valid():
post = form2.save(commit=False)
post.save()
return redirect('canditate_list')
else:
form2 = EvalForm()
return render(request, 'app/evaluation.html', {'criterions': form2,})
def register(request):
if request.method == "POST":
form = CandForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.save()
return redirect('candidate_detail', pk=post.pk)
else:
form = CandForm()
return render(request, 'app/register.html', {'form': form})
[编辑]:
我尝试按照以下方法:How do I drop a table from SQLite3 in DJango?提示但始终存在相同的错误。
答案 0 :(得分:0)
Django按照约定{app_label}_{model_name}
(小写)构造db表名。所以它指的是Evaluation
模型。问题出在avarage
模型中,您试图在模型中使用Evaluation.objects.get()
,但此时尚未在数据库中创建评估模型表。我相信你想建立一种关系,但这不是关系如何运作,你需要的是ForeignKey