I just thinking out loud here but: what if another thread does something to 
the previously loaded object? I'm not sure if Django has concurrency tests 
build into the test suite, but if it hasn't it could explain why the code 
is written as it is.

On Friday, 30 November 2012 19:53:32 UTC+1, Anssi Kääriäinen wrote:
>
> I would like to change the implementation of .save(). In the following 
> case: 
>
> s = SomeModel.objects.get(pk=somepk) 
> s.somefield = val 
> s.save() 
>
> we currently do this: 
>
> SELECT (from .get()) 
> SELECT (from .save()) 
> UPDATE 
>
> The second SELECT is redundant. We have knowledge that the model was 
> loaded from the same DB in s._state. So, we could instead do just: 
> SELECT (from .get()) 
> UPDATE 
>
> If the UPDATE doesn't match anything (due to concurrent delete for 
> example), then we can still do an INSERT. So, from user perspective 
> the behaviour isn't changed. 
>
> The force_update flag isn't doing the exact same thing - when using 
> force_update and the UPDATE doesn't change anything the INSERT isn't 
> done, instead an error is risen. 
>
> In most UPDATE situations this should reduce one SELECT from 
> model.save(). 
>
> The only problem I can see is that we have very explicitly documented 
> how .save() behaves. The generated queries are documented. I don't 
> consider the generated queries part of the publlic API, just the 
> behaviour. 
>
> All tests on all core backends do pass using the "UPDATE, if not 
> updated INSERT" behaviour. See ticket #16649 for patch & details. 
>
> Any objections moving forward with this plan? 
>
>  - Anssi 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/pDqtkw-B5v0J.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to