Douglas da Dias Silva
Douglas da Dias Silva

Reputation: 1230

Django: Error trying to save form

Hi guys I have a problem when y try to save the date of a forms in database:

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

Yes, I run manage.py makemigrations and manage.py migrate, I try to delete the folder migrations and run the commands once again, but the error continues...

So, here have my codes:

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

the template called in this 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>

Upvotes: 1

Views: 566

Answers (2)

Douglas da Dias Silva
Douglas da Dias Silva

Reputation: 1230

Well guys I got, what I did was:

1 - Remove the all migrations files in the 'migrations' folder except init.py

2 - delete the db.sqlite3 (in my case).

3 run python manage.py makemigrations and python manage.py migrate again.

I hope I have helped.

Upvotes: 0

wobbily_col
wobbily_col

Reputation: 11879

If you have deleted the migration files, there will still be an entry in the django_migrations table corresponding to the migrations that you have run. Look in there and delete the entries back to where it was working (before you added the 'nota' column).

When you run make migrations it creates the migration file, but doesn't touch the database. When you run migrate, it uses the migration file to generate SQL to alter the database tables. It logs all the migrations that have been run in the django_migrations table. If you have already deleted the migrations folder, then delete all the migrations in that table related to your application (there will also be migrations related to Django, such as auth tables - don't delete those).

Upvotes: 1

Related Questions