DecimalField returns 'This value must be a decimal number' when blank

1 841 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

elcaiaimar

nieprzeczytany,
16 lis 2014, 12:51:4316.11.2014
do django...@googlegroups.com
Hello everybody, I've a problem with DecimalField at Forms. I've a long form in my template and I want to left some fields in blank. I've declared these fields with
required=False, but when I submit the form I receive this error: 'This value must be a decimal number'.

I've tried to declare blank=True, null=True in respective fields in models.py and make a syncdb but it continues without works

Does somebody know how could I solve this problem?

Thank you very much!

Vijay Khemlani

nieprzeczytany,
16 lis 2014, 20:07:5516.11.2014
do django...@googlegroups.com
I'm not sure if a syncdb changes the null attribute of a field at the DB level, I think you need to make a migration for that.

Either way, it would be useful if you could post the declarations of the form and the model to see if everything is ok.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/95da2ed1-e9ce-43c5-9e53-3d17cab471d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wiadomość została usunięta

fred....@gmail.com

nieprzeczytany,
17 lis 2014, 13:15:2717.11.2014
do django...@googlegroups.com
I think you'd better have a look at the raw db sql. I remember that the syncdb won't change the null attr. of a column if it's not set when initialized. So two possible solutions: 1) set the db manually 2) use a tool like south to do a migration. Hope I can help you.

elcaiaimar

nieprzeczytany,
17 lis 2014, 16:01:0517.11.2014
do django...@googlegroups.com
Hello! I've tried to do a makemigrations and a migrate but It returns "No changes detected" and "No migrations to apply".
Sorry Vijay Khemlani, I forgot it, you have my code here:

forms.py:

class PozosForm(forms.Form):
    codpozo = forms.CharField(max_length=20)
    coorx = forms.DecimalField(max_digits=13, decimal_places=5)
    coory = forms.DecimalField(max_digits=13, decimal_places=5)
    tipo = forms.CharField(max_length=20, required=False)
    cotatrapa = forms.DecimalField(max_digits=6, decimal_places=2, required=False)
    profundidad = forms.DecimalField(max_digits=6, decimal_places=2, required=False)
    cotafondo = forms.DecimalField(max_digits=6, decimal_places=2, required=False)
    material = forms.ChoiceField(choices=MATERIAL_POZOS, required=False)
    materialpates = forms.ChoiceField(choices=MATERIAL_PATES, required=False)
    diametro = forms.DecimalField(max_digits=20, decimal_places=2, required=False)
    largotrapa = forms.DecimalField(max_digits=20, decimal_places=2, required=False)
    seccionmayor = forms.DecimalField(max_digits=5, decimal_places=0, required=False)
    seccionmenor = forms.DecimalField(max_digits=5, decimal_places=0, required=False)
    numacometidas = forms.DecimalField(max_digits=2, decimal_places=0, required=False)

models.py:

class Pozo(models.Model):
    # gid_pozo = models.IntegerField(primary_key=True)
    # gid_colector = models.ForeignKey(Colector)
    codpozo = models.CharField(max_length=20)
    coorx = models.DecimalField(max_digits=13, decimal_places=5)
    coory = models.DecimalField(max_digits=13, decimal_places=5)
    tipo = models.CharField(max_length=20)
    cotatrapa = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    profundidad = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    cotafondo = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    material = models.CharField(max_length=20)
    materialpates = models.CharField(max_length=20)
    diametro = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)
    largotrapa = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)
    seccionmayor = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)
    seccionmenor = models.DecimalField(max_digits=5, decimal_places=0, blank=True, null=True)
    numacometidas = models.DecimalField(max_digits=2, decimal_places=0, blank=True, null=True)

I think everything is ok, but if you find errors, please tell me. If not, I will try to use south.

Thank you very much!

elcaiaimar

nieprzeczytany,
20 lis 2014, 10:44:2620.11.2014
do django...@googlegroups.com
I' ve seen that south is for older django versions than 1.7, I continue having the same problem, I wonder if the issue will be in my views, when I save all the information, please, have a look to my code:

if 'formulariopozo' in request.POST:
        formulario = PozosForm(request.POST)
        if formulario.is_valid():
            titulo = 'Formulario de pozos'

            codpozo=request.POST['codpozo']
            x=request.POST['coorx']
            y=request.POST['coory']
            tipo=request.POST['tipo']
            cotatrapa=request.POST['cotatrapa']
            profundidad=request.POST['profundidad']
            cotafondo=request.POST['cotafondo']
            material=request.POST['material']
            materialpates=request.POST['materialpates']
            diametro=request.POST['diametro']
            largotrapa=request.POST['largotrapa']
            seccionmayor=request.POST['seccionmayor']
            seccionmenor=request.POST['seccionmenor']
            numacometidas=request.POST['numacometidas']
            origen=request.POST['origen']
            observaciones=request.POST['observaciones']

            pnt=Point(float(x),float(y))
            xy=Pozo(codpozo=codpozo, coorx=x, coory=y, tipo=tipo, cotatrapa=cotatrapa,
            profundidad=profundidad, cotafondo=cotafondo, material=material,
            materialpates=materialpates, diametro=diametro, largotrapa=largotrapa,
            seccionmayor=seccionmayor, seccionmenor=seccionmenor, numacometidas=numacometidas,
            origen=origen, observaciones=observaciones, geom=pnt)
           
            xy.save()

I've tried to put default=None or default=Decimal('0.00') in my models.py but it continues without working. I don't know what can I do

Thank you very much


El domingo, 16 de noviembre de 2014 18:51:43 UTC+1, elcaiaimar escribió:

Collin Anderson

nieprzeczytany,
21 lis 2014, 17:39:4921.11.2014
do django...@googlegroups.com
Hi,

Could you post a traceback? Also, what database are you using?

Also also, ModelForms would help simplify your code. Check them out if you haven't.

Thanks,
Collin

elcaiaimar

nieprzeczytany,
22 lis 2014, 12:57:1322.11.2014
do django...@googlegroups.com
Hi Collin

I'm using postgresql db, I' ve attached my views, forms and models. I' ve thought to do the ModelForm when I solve this problem, thanks for your advice!

Thank you very much!



El domingo, 16 de noviembre de 2014 18:51:43 UTC+1, elcaiaimar escribió:
forms.py
models.py
views.py

Collin Anderson

nieprzeczytany,
24 lis 2014, 20:23:0924.11.2014
do django...@googlegroups.com
Hi,

Use form.cleaned_data instead of request.POST, because form.cleaned_data will property convert strings into decimals or None.

codpozo = form.cleaned_data['codpozo']

But again, use a ModelForm. it's super easy:

class PozoForm(forms.ModelForm):
   
class Meta:
        model
= Pozo
        fields
= '__all__'

def EdicionCuenca(request):
   
if 'pozoform' in request.POST:
        pozoform
= PozoForm(request.POST)
        if pozoform.is_valid():
            pozo
= PosoForm.save()
           
return redirect('/edicioncuenca/')
   
else:
        pozoform
= PozoForm()
   
# etc

Collin

elcaiaimar

nieprzeczytany,
25 lis 2014, 07:25:3425.11.2014
do django...@googlegroups.com
It works! You are great Collin! Thank you very much!


El domingo, 16 de noviembre de 2014 18:51:43 UTC+1, elcaiaimar escribió:
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0