I posted on the ticket before reading this thread. Whoops. As mentioned above it's impossible to always convert user input into the output provided by the database without doing a round trip, because there are driver data adapters, and django converters that both get a chance at modifying the data as it flows through the system. We need to do a better job of highlighting this limitation in some way.
On Tuesday, 14 February 2017 20:40:00 UTC+11, Marc Tamlyn wrote: > > Agreed - ensuring that this works in the general case is not reliable. > > On 13 February 2017 at 15:51, Adam Johnson <m...@adamj.eu <javascript:>> > wrote: > >> What do you think? Absent a better suggestion, we could document this >>> pitfall so that there's something to point to when related tickets come in. >> >> >> +1, as Aymeric points out the more complex fields probably won't work >> with coercion-on-set. >> >> On 13 February 2017 at 15:27, charettes <chare...@gmail.com <javascript:> >> > wrote: >> >>> > What do you think? Absent a better suggestion, we could document this >>> pitfall so that there's something to point to when related tickets come in. >>> >>> I also think this is the most appropriate solution. >>> >>> Le lundi 13 février 2017 09:57:49 UTC-5, Tim Graham a écrit : >>>> >>>> Once in a while, there's a ticket about this behavior: >>>> >>>> m = Model(decimal='12.9') >>>> m.save() >>>> self.assertEqual(m.decimal, '12.9') >>>> m.refresh_from_db() >>>> self.assertEqual(m.decimal, Decimal('12.9')) >>>> >>>> That is, you can create a model with an incorrect type and it won't be >>>> fixed until you refresh the object from the database. >>>> >>>> Most recent complaint, "it is only a basic expectation that the DB >>>> layer and the Application layer will correspond to each-other after >>>> performing save, which is in other words, syncing your state with the >>>> DB. Personally, this bug (one way binding between application and db on >>>> save) broke many of my tests and took a lot of my time." [0] See [1] for >>>> another manifestation of this. >>>> >>>> I think calling to_python() in Model.save() would have unacceptable >>>> performance consequences for little benefit considering that it's a >>>> reasonable expectation for developers to provide the correct type. >>>> Further, >>>> you can use full_clean() to coerce to the correct types: >>>> >>>> m = Model(decimal='12.9') >>>> m.full_clean() >>>> self.assertEqual(m.decimal, Decimal('12.9')) >>>> >>>> What do you think? Absent a better suggestion, we could document this >>>> pitfall so that there's something to point to when related tickets come in. >>>> >>>> [0] https://code.djangoproject.com/ticket/27825 >>>> [1] https://code.djangoproject.com/ticket/24028 >>>> >>> -- >>> 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 <javascript:>. >>> To post to this group, send email to django-d...@googlegroups.com >>> <javascript:>. >>> Visit this group at https://groups.google.com/group/django-developers. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-developers/4e59974e-9916-434e-8840-4f42996e5052%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/django-developers/4e59974e-9916-434e-8840-4f42996e5052%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> Adam >> >> -- >> 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 <javascript:>. >> To post to this group, send email to django-d...@googlegroups.com >> <javascript:>. >> Visit this group at https://groups.google.com/group/django-developers. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-developers/CAMyDDM1B0RJa928Yvk0uXenU009tsU%3DFDtPW_u2gmpgXTVpdFQ%40mail.gmail.com >> >> <https://groups.google.com/d/msgid/django-developers/CAMyDDM1B0RJa928Yvk0uXenU009tsU%3DFDtPW_u2gmpgXTVpdFQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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 post to this group, send email to django-developers@googlegroups.com. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/471dde73-6dea-49b9-87d2-44a943e102b5%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.