gpt4 book ai didi

python-3.x - Django : Error trying to save form

转载 作者:行者123 更新时间:2023-12-03 07:58:42 24 4
gpt4 key购买 nike

大家好,当您尝试将表单的日期保存在数据库中时,我遇到了一个问题:

django.db.utils.OperationalError: table app_avaliacao has no column named nota



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

所以,这里有我的代码:

模型.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

View .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})

此 View 中调用的模板:
<!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>

最佳答案

如果您删除了迁移文件, 中仍然会有一个条目。 django_migrations 与您已运行的迁移相对应的表。在那里查看并将条目删除回它正在工作的位置(在添加“nota”列之前)。

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

关于python-3.x - Django : Error trying to save form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42826964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com