Hi!
I would like to provide an example of why this field is needed.
In PostgreSQL, a floating point literal like 4.6 has type decimal, not 
binary floating point (see SELECT pg_typeof(4.6)).
When comparing binary floating point values to decimals, the decimal is 
converted to double precision binary floating point value. This make 
FloatField work.
But if you have real/float4 fields and you compare them to decimal value, 
it doesn't work.
Here is an example, with a workaround.

in bar/models.py

from django.db import models
from django.db.models import FloatField

class RealField(FloatField):
    def db_type(self, connection):
        return 'real'

class Thing(models.Model):
    score = RealField(null=False)

and now in shell:

from django.db.models import Value
from django.db.models.functions import Cast
from bar.models import RealField, Thing

Thing(score=4.6).save()

Thing.objects.all()[0].score
4.6

Thing.objects.filter(score=4.6).first()
None

str(Thing.objects.filter(score=4.6).query)
SELECT "bar_thing"."id", "bar_thing"."score" FROM "bar_thing" WHERE 
"bar_thing"."score" = 4.6

Thing.objects.filter(score=Cast(Value(4.6), RealField())).first()
<Thing: Thing object (1)>

str(Thing.objects.filter(score=Cast(Value(4.6), RealField())).query)
SELECT "bar_thing"."id", "bar_thing"."score" FROM "bar_thing" WHERE 
"bar_thing"."score" = (4.6)::real

Can Django have a RealField that automatically does the cast?


-- 
 
<https://swampup.jfrog.com/?utm_source=email&utm_medium=footer&utm_campaign=emea_sales&utm_term=city-tour-22>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/068b4121-d28c-4bd7-997e-7fc5d499119en%40googlegroups.com.
  • Ti... 'Zeev Tarantov' via Django developers (Contributions to Django itself)
    • ... Jörg Breitbart

Reply via email to