Django - Error saving to Database

2

The data is not being saved in the Database and Django is not generating Error, but when I create an object of Livro and try to save by the shell it returns me the following error:

  

ValueError: invalid literal for int () with base 10:

I tried to search for solutions but to no avail! I'm using sqlite3! Django 1.6.2 and python 2.7! The project is available in github .

Models.py:

class Biblioteca(models.Model):

    endereco = models.CharField(max_length = 80)
    nome = models.CharField(max_length = 60)
    telefone = models.IntegerField(max_length = 11)

    def __unicode__(self):
        return self.nome

class Livro(models.Model):
    codigo = models.CharField(max_length = 10)
    publicacao = models.IntegerField()
    autor = models.CharField(max_length = 80)
    editora = models.CharField(max_length = 45)
    genero = models.CharField(max_length = 45)
    sinopse = models.CharField(max_length=150)
    titulo = models.CharField(max_length = 150)
    biblioteca = models.ForeignKey(Biblioteca)

    def __unicode__(self):
        return (self.titulo, self.autor)

Forms.py:

class FormLivro(forms.Form):

    codigo = forms.CharField(max_length=10)
    publicacao = forms.IntegerField()
    autor = forms.CharField(max_length=80)
    editora = forms.CharField(max_length=45)
    genero = forms.CharField(max_length=45)
    sinopse = forms.CharField(max_length=150)
    titulo = forms.CharField(max_length=150)

def save(self):

    codigo = self.cleaned_data.get('codigo')# Acessando os Fields
    publicacao = self.cleaned_data.get('publicacao')         
    autor = self.cleaned_data.get('autor')
    editora = self.cleaned_data.get('editora')
    genero = self.cleaned_data.get('genero')
    sinopse = self.cleaned_data.get('sinopse')
    titulo = self.cleaned_data.get('titulo')

    novo_livro = Livro(
        codigo = codigo,
        publicacao = publicacao,
        autor = autor,
        editora = editora,
        genero = genero,
        sinopse = sinopse,
        titulo = titulo
    )
    novo_livro.save()
    return novo_livro 

def clean_codigo(self):
    codigo = self.cleaned_data.get('codigo')
    if Livro.object.filter(email = email):
        raise forms.ValidationError('Codigo já cadastrado!')
    return codigo

Views.py:

def cadLivro(request):

    livros = Livro.objects.all() # Lista de livros
    if request.method == "POST":
        form = FormLivro(request.POST)
        if form.is_valid(): # Processando o Formulario
            novo_livro = form.save()
            HttpResponseRedirect(reverse('nCadLivro'))
    else:
        form = FormLivro()

    return render(request, 'cadastro_livro.html')
    
asked by anonymous 13.05.2014 / 22:50

1 answer

1

Your code in Github is different from the question code. In the Livro template, the codigo field is as IntegerField instead of CharField . For this reason, the mentioned error occurs when trying to save the Livro to the shell. But using a numeric value in this field, it normally saves (either using an integer, or a string representing an integer in base 10).

I tried to test the view too, but it does not even compile, so I suggest solving this (and other) problem if you need help with it too. And always remember to check if the database is consistent with the models - because if you modify some field in the model, running syncdb again will not do anything, you have to destroy your table and create it again (for example using reset ; caution: this erases all data from the table). Soon, Django will support schema migration, but at the moment this is not done natively (only by external libraries such as the South).

P.S. Your __unicode__ method does not work because it returns a tuple instead of a string. I suggest changing it to (for example):

def __unicode__(self):
    return unicode((self.titulo, self.autor))
    
14.05.2014 / 00:18