Django:尝试保存表单时出错

时间:2017-03-16 06:41:06

标签: python-3.x sqlite django-models django-forms

大家好我在尝试在数据库中保存表单的日期时遇到问题:

  

django.db.utils.OperationalError:表app_avaliacao没有列   名为nota

是的,我运行 manage.py makemigrations manage.py migrate ,我尝试删除文件夹迁移并运行命令一次再次,但错误继续......

所以,这里有我的代码:

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 self

views.py:

from django.shortcuts import render, get_object_or_404
from .models import Candidato, Criterio
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):
    #criterios = Criterio.objects.all()
    if request.method == "POST":
        form2 = AvalForm(request.POST)
        if form2.is_valid():
            post = form2.save(commit=False)
            post.save()
            return redirect('canditato_list', pk=post.pk)
    else:
        form2 = AvalForm()
        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})

在此视图中调用的模板:

<!DOCTYPE html></!DOCTYPE html>
<html>
<head>
    <title>Avaliação</title>
</head>
<body>
<h1>Avaliação</h1>
    <form method="POST" class="post-form">
        {% csrf_token %}
        {{ criterios.as_p }}
        <button type="submit" class="save btn btn-default">Guardar</button>
    </form>
</body>
</html>
</html>

2 个答案:

答案 0 :(得分:1)

如果您删除了迁移文件,则 django_migrations 表中仍会有与您运行的迁移相对应的条目。在那里查看,并将条目删回到它工作的位置(在添加&#39; nota&#39;列之前)。

当您运行make migrations时,它会创建迁移文件,但不会触及数据库。运行迁移时,它使用迁移文件生成SQL以更改数据库表。它记录已在django_migrations表中运行的所有迁移。如果您已经删除了迁移文件夹,则删除该表中与您的应用程序相关的所有迁移(还将有与Django相关的迁移,例如auth表 - 不要删除它们)。

答案 1 :(得分:0)

我得到了好伙伴,我做的是:

1 - 删除“迁移”文件夹中的所有迁移文件,但 init .py

除外

2 - 删除db.sqlite3(在我的情况下)。

3再次运行 python manage.py makemigrations python manage.py migrate

我希望我有所帮助。