> Can Django have a RealField that automatically does the cast?
I dont think it is a good idea to go that route in django and add that
as a standard django field, mainly for these reasons:
- FloatField happens to align correctly between pythons only "native"
float type and the chosen db counterparts (as both sides use double
precision under the hood), for a single precision field type that
congruence is not given anymore leading to even more errors
- there are database systems that only do double precision (sqlite I think)
- single precision is an edge case and quite hard to get maths running
with in python (you'd need to shim everything either with ctypes or
numpy, or write your own C-binding)
The last point raises the question - why should django provide a field
type, thats not even represented in its language? To me the django ORM
never felt like a database feature exporter at any cost, but like a
convenient way to interact with databases within typical python
paradigms. Here python clearly falls short lacking a single precision
type, so there are no common use cases at all for this. And mixing
precisions is never a good idea, as your example already showed.
Last but not least - you can still create a single precision float field
yourself, but you should also write a corresponding python type, so you
can do proper maths with it. In your field class you can use
get_db_prep_value or get_prep_value to customize the needed SQL repr
(eg. append inline cast), or use a custom psycopg OID adapter for your
python type.
Cheers,
jerch
Am 25.08.22 um 20:39 schrieb 'Zeev Tarantov' via Django developers
(Contributions to Django itself):
>
https://swampup.jfrog.com/?utm_source=email&utm_medium=footer&utm_campaign=emea_sales&utm_term=city-tour-22
> <
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-develop...@googlegroups.com
> <mailto:
django-develop...@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
> <
https://groups.google.com/d/msgid/django-developers/068b4121-d28c-4bd7-997e-7fc5d499119en%40googlegroups.com?utm_medium=email&utm_source=footer>.