Re: Pretty Django model instaces updating

2012-10-09 Thread Kee
I also propose these two little helpers, one is ``refresh()`` and other is 
``update()``
https://gist.github.com/3859962

``update()`` updates instance without calling ``save()`` and calling 
signals and returns updated instance (sometimes you want to update instance 
without involving save signals)

This helpers are particularly useful in writing tests, may be makes sense 
including them not to ``djanog.db.models`` but to ``django.test`` or 
``django.test.tools``.

-- 
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/-/FxGwLxwM8SAJ.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Jeremy Dunck
On Thu, Mar 1, 2012 at 9:28 PM, Carl Meyer  wrote:

> The "only_fields" kwarg suggestion comes from ticket 4102, and it is
> primarily intended as a performance optimization for large models by
> only sending the values of certain fields to the database at all,
> something that your proposed code does not do.

It is also useful in the case where you have fields on the model which
have authoritatively changed, but have other local values which might
be stale.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Anssi Kääriäinen
On Friday, March 2, 2012 7:28:30 AM UTC+2, Carl Meyer wrote:
>
> On 03/01/2012 09:46 PM, Tai Lee wrote:
> > This is easy to achieve in your own code. Create your own BaseModel
> > class that all your models subclass. Instead of using
> > `.filter().update()`, do this:
> > 
> > def update(self, **kwargs):
> > """
> > Updates the fields specified in `kwargs` and then call `save()`.
> > """
> > if kwargs:
> > for k, v in kwargs.iteritems():
> > setattr(self, k, v)
> > self.save(force_update=True)
> > 
> > This will still fire the `save` signals, and your model instance will
> > have the new values as well as having them saved to the database.
> > 
> > I think it is also easier than assigning values to each field and then
> > calling `.save(only_fields[field1, field2])`, which I don't think is
> > any better than filter().update(). If `save(only_fields=...)` were to
> > only write specified fields to the database, this might cause
> > unexpected side effects with `save` signals?
>
> The "only_fields" kwarg suggestion comes from ticket 4102, and it is
> primarily intended as a performance optimization for large models by
> only sending the values of certain fields to the database at all,
> something that your proposed code does not do.
>
I should have mentioned that ticket. This is one of the tickets where the 
feature isn't hard to implement. Getting the API exactly correct is hard. 
And keeping the feature restricted is even harder...

 - 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/-/aqdLL7PP1AsJ.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Carl Meyer
On 03/01/2012 09:46 PM, Tai Lee wrote:
> This is easy to achieve in your own code. Create your own BaseModel
> class that all your models subclass. Instead of using
> `.filter().update()`, do this:
> 
> def update(self, **kwargs):
>   """
>   Updates the fields specified in `kwargs` and then call `save()`.
>   """
>   if kwargs:
>   for k, v in kwargs.iteritems():
>   setattr(self, k, v)
>   self.save(force_update=True)
> 
> This will still fire the `save` signals, and your model instance will
> have the new values as well as having them saved to the database.
> 
> I think it is also easier than assigning values to each field and then
> calling `.save(only_fields[field1, field2])`, which I don't think is
> any better than filter().update(). If `save(only_fields=...)` were to
> only write specified fields to the database, this might cause
> unexpected side effects with `save` signals?

The "only_fields" kwarg suggestion comes from ticket 4102, and it is
primarily intended as a performance optimization for large models by
only sending the values of certain fields to the database at all,
something that your proposed code does not do.

Carl



signature.asc
Description: OpenPGP digital signature


Re: Pretty Django model instaces updating

2012-03-01 Thread Tai Lee
This is easy to achieve in your own code. Create your own BaseModel
class that all your models subclass. Instead of using
`.filter().update()`, do this:

def update(self, **kwargs):
"""
Updates the fields specified in `kwargs` and then call `save()`.
"""
if kwargs:
for k, v in kwargs.iteritems():
setattr(self, k, v)
self.save(force_update=True)

This will still fire the `save` signals, and your model instance will
have the new values as well as having them saved to the database.

I think it is also easier than assigning values to each field and then
calling `.save(only_fields[field1, field2])`, which I don't think is
any better than filter().update(). If `save(only_fields=...)` were to
only write specified fields to the database, this might cause
unexpected side effects with `save` signals?

If you don't like the name `update`, or it clashes with something else
in your model namespaces, just use something else.

I don't think this warrants inclusion in Django core.

Cheers.
Tai.


On Mar 2, 8:48 am, Kee  wrote:
> Fully agree with Anssi Kääriäinen,
>
> the best approach is ``only_fields`` for save()``
>
> On Mar 1, 11:45 pm, Anssi Kääriäinen  wrote:
>
>
>
>
>
>
>
> > On Thursday, March 1, 2012 9:29:34 PM UTC+2, Carl Meyer wrote:
>
> > > 
>
> > > Thanks for the suggestion. I agree that that's a useful pattern and one
> > > I frequently use myself. The problem is that the model instance
> > > namespace is a precious resource, and using up any more of it than we
> > > already do can easily lead to backwards-compatibility problems. In this
> > > case, I don't think turning a one-liner into a shorter one-liner
> > > justifies using up more of that namespace.
>
> > > Carl
>
> > I suggest implementing .save(only_fields=list_of_fields).
>
> > That should not be hard to implement and will not consume the namespace of
> > models. Signals are fired properly. In addition, update() would be a bit
> > strange instance method: it updates the specified fields by the kwargs (the
> > instance's field values do not matter). In addition, after the update the
> > instance's fields are not updated to reflect what was just saved.
>
> >  - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Kee
Fully agree with Anssi Kääriäinen,

the best approach is ``only_fields`` for save()``

On Mar 1, 11:45 pm, Anssi Kääriäinen  wrote:
> On Thursday, March 1, 2012 9:29:34 PM UTC+2, Carl Meyer wrote:
>
> > 
>
> > Thanks for the suggestion. I agree that that's a useful pattern and one
> > I frequently use myself. The problem is that the model instance
> > namespace is a precious resource, and using up any more of it than we
> > already do can easily lead to backwards-compatibility problems. In this
> > case, I don't think turning a one-liner into a shorter one-liner
> > justifies using up more of that namespace.
>
> > Carl
>
> I suggest implementing .save(only_fields=list_of_fields).
>
> That should not be hard to implement and will not consume the namespace of
> models. Signals are fired properly. In addition, update() would be a bit
> strange instance method: it updates the specified fields by the kwargs (the
> instance's field values do not matter). In addition, after the update the
> instance's fields are not updated to reflect what was just saved.
>
>  - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Anssi Kääriäinen


On Thursday, March 1, 2012 9:29:34 PM UTC+2, Carl Meyer wrote:
>
> 
>
> Thanks for the suggestion. I agree that that's a useful pattern and one
> I frequently use myself. The problem is that the model instance
> namespace is a precious resource, and using up any more of it than we
> already do can easily lead to backwards-compatibility problems. In this
> case, I don't think turning a one-liner into a shorter one-liner
> justifies using up more of that namespace.
>
> Carl
>
I suggest implementing .save(only_fields=list_of_fields).

That should not be hard to implement and will not consume the namespace of 
models. Signals are fired properly. In addition, update() would be a bit 
strange instance method: it updates the specified fields by the kwargs (the 
instance's field values do not matter). In addition, after the update the 
instance's fields are not updated to reflect what was just saved.

 - 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/-/t7tJffdFeg4J.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Meir Kriheli
Hi,

On Thu, Mar 1, 2012 at 9:29 PM, Carl Meyer  wrote:

> Hi Kee,
>
> On 03/01/2012 11:24 AM, Kee wrote:
> > This helper could be used as an instance method in
> > django.core.models.Model class:
> >  https://gist.github.com/1951748
>
> Thanks for the suggestion. I agree that that's a useful pattern and one
> I frequently use myself. The problem is that the model instance
> namespace is a precious resource, and using up any more of it than we
> already do can easily lead to backwards-compatibility problems. In this
> case, I don't think turning a one-liner into a shorter one-liner
> justifies using up more of that namespace.
>
> Carl
>
>
IIRC, it won't call the instance's save() method nor fire model related
signals,
which isn't the expected behaviour from an instance method.

Cheers
-- 
Meir Kriheli
http://meirkriheli.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
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.



Re: Pretty Django model instaces updating

2012-03-01 Thread Carl Meyer
Hi Kee,

On 03/01/2012 11:24 AM, Kee wrote:
> This helper could be used as an instance method in
> django.core.models.Model class:
>  https://gist.github.com/1951748

Thanks for the suggestion. I agree that that's a useful pattern and one
I frequently use myself. The problem is that the model instance
namespace is a precious resource, and using up any more of it than we
already do can easily lead to backwards-compatibility problems. In this
case, I don't think turning a one-liner into a shorter one-liner
justifies using up more of that namespace.

Carl



signature.asc
Description: OpenPGP digital signature


Pretty Django model instaces updating

2012-03-01 Thread Kee
This helper could be used as an instance method in
django.core.models.Model class:
 https://gist.github.com/1951748

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
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.