Like it has been discussed a while ago [1] about adding db_default, we should stick with something similar to that and support updates as well.
My 2 cents here.
I like the idea Anssi has proposed to delegate as much as possible using expressions. So I would propose similar to what discussed in the other thread using updated_at = DateTimeField(db_default=Expression, db_update=Expression) (I don't like the param name db_update but you got the idea).
Updating existing records. I would opt for leaving out fields which have a db_update parameter, but still allow to override this behavior by specifying those fields explicitly instance.save(update_fields=...). And the same for MyModel.objects.filter().update(**fields) we should not include so called auto fields in the update statement but we should never leave out a field explicitly specified, (we already have problems how to change pk values on existing records, and we don't want more behavior like that).
We should refresh values for records on insert and update using the RETURNING clause for those databases which support it. And defer fetching the new value for other databases and for update only, since for insert we already have to fetch the pk anyway, so we can fetch the other values as well. An interesting idea like Anssi proposed would be to allow refreshing values behavior be controlled via the Expression.
For queryset methods like bulk_create and update we can already control deferring values for fields using .defer() and .only(). So if one would really want to save the extra overhead fetching new values from db after update, this can be done by MyModel.objects.get(pk=instance.pk).update().
I don't like Field(db_computed=True) it adds very minimal control over how and what that does.
To sum up:
1) fields should have flags how they are used in insert / update queries.
2) controlling behavior should be done via Expressions
3) Field api should add 2 attributes db_default and db_update (perhaps some better param name)
4) Do not limit overriding save behavior on a per query bases.
[1] https://groups.google.com/forum/#!msg/django-developers/3mcro17Gb40/NPINCcyyBAAJ
--
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.
To post to this group, send email to django-d...@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/2ac56d4e-9259-4fa6-985b-4311686662b6%40googlegroups.com.
For the update_fields change, I think we can do that completely separately. The same goes for changing the way how deferred fields are implemented.
Model.__init__()
will be called with value DEFERRED for those fields which are deferred. Previously those fields were not given to __init__()
at all.Before doing any further work on this we should decide if the Model.__init__() problem is bad enough to stop this cleanup, and if so, do anybody have any ideas of how to avoid the problem?
* Unless I miss something, YourModel.__init__ is Model.__init__ if the user didn't change it -> pass is DEFERRED
Oh, I somewhat missread and though there would be a new DEFERRED argument, the backwards issue is easy enough though:
* Unless I miss something, YourModel.__init__ is Model.__init__ if the user didn't change it -> pass is DEFERRED
* If the user changed it check for model._meta.new_style_deferred and continue accordingly
* Raise a warning if the __init__ is a custom one and new_style_deffered is not set…
The proposal therefore is to add a `readonly` option to the base `Field` class that when `True` would strip these fields from being compiled to SQL during `INSERT`s and `UPDATE`s. This allows for a very simple change that covers all possible write queries that Django may perform (including bulk_*).
There exists a proof of concept https://github.com/novafloss/django-readonly-field
--
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+unsubscribe@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/3db52e5b-54db-4736-845b-58a22b6f015c%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.