Decimal value allowed for auto primary key

2014-10-25 Thread Kafex
If I have the following model...
class FruitType(models.Model):
  name = models.CharField(max_length=255)

And I insert some records...
FruitType.objects.create(name="Apple")
FruitType.objects.create(name="Orange")

Why is this valid?
FruitType.objects.get(id=1.2) # Gets me the apple
FruitType.objects.get(id=2.9) # Gets me the apple


Django seems to truncate the id to just the integer part.
This also applies to form validation:
class FruitForm(forms.Form):
  type = forms.ModelChoiceField(queryset=FruitType.objects.all())

form = FruitForm(data={'type': 1.5})
form.is_valid() # returns True

Is there any way to prevent this? I.e., I want my form to treat 1.5 as 
invalid input and raise a ValidationError.

Thank you.


-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/9f495b17-6f48-485b-9a6a-75f9e83f59b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Decimal value allowed for auto primary key

2014-10-25 Thread Kafex
Whoops... just to clarify...

FruitType.objects.get(id=2.9) # Gets me the apple

I meant to comment that this gets me the orange.


On Saturday, October 25, 2014 8:37:55 PM UTC-4, Kafex wrote:
>
> If I have the following model...
> class FruitType(models.Model):
>   name = models.CharField(max_length=255)
>
> And I insert some records...
> FruitType.objects.create(name="Apple")
> FruitType.objects.create(name="Orange")
>
> Why is this valid?
> FruitType.objects.get(id=1.2) # Gets me the apple
> FruitType.objects.get(id=2.9) # Gets me the apple
>
>
> Django seems to truncate the id to just the integer part.
> This also applies to form validation:
> class FruitForm(forms.Form):
>   type = forms.ModelChoiceField(queryset=FruitType.objects.all())
>
> form = FruitForm(data={'type': 1.5})
> form.is_valid() # returns True
>
> Is there any way to prevent this? I.e., I want my form to treat 1.5 as 
> invalid input and raise a ValidationError.
>
> Thank you.
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/07857da5-7abf-478c-8f17-be1924e63541%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Decimal value allowed for auto primary key

2014-10-26 Thread Kafex
Ok, that makes sense, thanks for the reply!

On Sunday, October 26, 2014 7:34:06 PM UTC-4, Tom Evans wrote:
>
> On Sun, Oct 26, 2014 at 12:37 AM, Kafex > 
> wrote: 
> > If I have the following model... 
> > class FruitType(models.Model): 
> >   name = models.CharField(max_length=255) 
> > 
> > And I insert some records... 
> > FruitType.objects.create(name="Apple") 
> > FruitType.objects.create(name="Orange") 
> > 
> > Why is this valid? 
> > FruitType.objects.get(id=1.2) # Gets me the apple 
> > FruitType.objects.get(id=2.9) # Gets me the apple 
> > 
> > 
> > Django seems to truncate the id to just the integer part. 
>
> Yes. The value is coerced to int, that is int(value) will be run on 
> it. If you pass a type that can be coerced to an int, no error will be 
> raised, so be aware of that. 
>
> > This also applies to form validation: 
> > class FruitForm(forms.Form): 
> >   type = forms.ModelChoiceField(queryset=FruitType.objects.all()) 
> > 
> > form = FruitForm(data={'type': 1.5}) 
> > form.is_valid() # returns True 
> > 
> > Is there any way to prevent this? I.e., I want my form to treat 1.5 as 
> > invalid input and raise a ValidationError. 
>
> This one is less obviously a problem, a typical form would actually be 
> passed the string '1.5' (not the float 1.5), which would in fact raise 
> a ValidationError. 
>
> Cheers 
>
> Tom 
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/ed2ab1b6-0e39-4be7-ac6f-9c3f0a74fd24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.