Reputation: 31
Models.py
class Produto(models.Model):
uuid = models.CharField(max_length=50, primary_key=True, editable=False, default=gen_uuid)
produto = models.CharField(max_length=100, unique=False, verbose_name="Item")
class Meta:
verbose_name = "Produto"
verbose_name_plural = "Produtos"
class Item(models.Model):
uuid = models.CharField(max_length=50, primary_key=True, editable=False, default=gen_uuid)
tipo = models.CharField(max_length=100, null=False, blank=True, unique=False, verbose_name="tipo_mtr")
class Meta:
verbose_name = "Item"
verbose_name_plural = "Itens"
class Orcamento(models.Model):
uuid = models.CharField(max_length=50, primary_key=True, editable=False, default=gen_uuid)
quantidade = models.IntegerField(max_length=10, unique=False, null=True, verbose_name="qtde")
produto = models.ForeignKey(Produto, verbose_name="Produto")
tipo = models.ManyToManyField(Item, verbose_name="Item")
class Meta:
verbose_name = "Orcamento"
verbose_name_plural = "Orcamentos"
unique_together = ("produto", "uuid")
def __unicode__(self):
return unicode(self.produto)
My forms.py (OrcamentoForm)
class OrcamentoForm(ModelForm):
tipo_id = ModelMultipleChoiceField(queryset=Item.objects.all(), required=True, widget=SelectMultiple(attrs={"style":"width:500px",}), help_text="Coloque o Tipo de Medida - Requerido")
quantidade = IntegerField(label="Quantidade", required=True, help_text="Coloque a Quantidade - Requerido")
produto_id = ModelMultipleChoiceField(queryset=Produto.objects.all(), widget=SelectMultiple(attrs={"style":"width:500px",}), required=True, help_text="Escolha o Produto - Requerido")
def __init__(self, *args, **kwargs):
super(OrcamentoForm, self).__init__(*args, **kwargs)
# without the next line label_from_instance does NOT work
self.fields['produto_id'].queryset = Produto.objects.all()
self.fields['produto_id'].label_from_instance = lambda Produto: "%s" % (Produto.produto)
self.fields['tipo_id'].queryset = Item.objects.all()
self.fields['tipo_id'].label_from_instance = lambda Item: "%s" % (Item.tipo)
class Meta:
model = Orcamento
fields = ["quantidade", "tipo_id", "produto_id" ]
My views.py
def orcamento(request):
form = OrcamentoForm(request.POST or None, request.FILES or None)
if request.method == 'POST':
form.save()
return render_to_response("webSite/teste.html", context_instance = RequestContext(request))
else:
return render_to_response("webSite/orcamento.html", {"form": form }, context_instance=RequestContext(request))
With if form.is_valid():
in views.py
, don't save form in table mysql.
Help-me :(
Upvotes: 3
Views: 19036
Reputation: 1
This error happened to me but I solved by adding parentheses to is_valid and it became is_valid() and the error went and never come back
Upvotes: 0
Reputation: 153
This error can also occur if you never included the ()
in .is_valid()
. Having the validity checker as .is_valid:
also raises the same error.
Upvotes: 0
Reputation: 3911
The same error occurred when I mistyped
.is_valid()
as .is_valied()
.
You might wanna check whether it's the case.
Upvotes: 0
Reputation: 4971
If form is not getting saved when you perform a check of form.is_valid()
means the form was invalid. In such cases, we should display the errors found by the form for every field. For that to happen, first you should pass form data to templates, if form is invalid.
#views.py
def orcamento(request):
form = OrcamentoForm(request.POST or None, request.FILES or None)
if request.method == 'POST':
if form.is_valid():
form.save()
return render_to_response("webSite/teste.html", context_instance = RequestContext(request))
#Following will run in all cases except when form was valid
return render_to_response("webSite/orcamento.html", {"form": form }, context_instance=RequestContext(request))
You can display all the errors thrown by the form or field-wise errors in your template. See the django documentation detailed understanding.
Upvotes: 4