Implementing Model.get_again, which fetches a fresh copy from the DB. #2194

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
Contributor

Wilfred commented Jan 21, 2014

This is a proposed fix to https://code.djangoproject.com/ticket/901

I believe that most users are implementing this functionality with the
following workaround:

my_foo = Foo.objects.get(pk=my_foo.pk)  # Get latest value from DB.

This method simply provides a standard Django way of reloading from the
DB, and includes documentation so users can discover it.

Several commentators have proposed a Model.reload() method that modifies
the model in place. However, this interacts badly with references, such
as OneToOneField. See
https://groups.google.com/d/msg/django-developers/Dcqq7zr2VZo/3OzSEoAS9_sJ

This method compromises by returning a new instance, which I believe
meets the large majority of use cases.

Implementing Model.get_again, which fetches a fresh copy from the DB.
This is a proposed fix to https://code.djangoproject.com/ticket/901

I believe that most users are implementing this functionality with the
following workaround:

   my_foo = Foo.objects.get(pk=my_foo.pk)  # Get latest value from DB.

This method simply provides a standard Django way of reloading from the
DB, and includes documentation so users can discover it.

Several commentators have proposed a Model.reload() method that modifies
the model in place. However, this interacts badly with references, such
as OneToOneField. See
https://groups.google.com/d/msg/django-developers/Dcqq7zr2VZo/3OzSEoAS9_sJ

This method compromises by returning a new instance, which I believe
meets the large majority of use cases.
Member

jarshwah commented Jan 24, 2014

After reading the discussion on the ML, I don't think this solves the intended use case. You're returning a new instance, but not updating the attributes of the current instance in-place - meaning references that have been handed off elsewhere will be 'stale'.

This reply in particular describes the difference between your use-case and the wanted one: https://groups.google.com/d/msg/django-developers/Dcqq7zr2VZo/vKwqMxxZHT4J

Owner

timgraham commented Feb 7, 2014

Agreed with @jarshwah, I also commented on the ticket itself. There's another patch on the ticket that looks more on the right track.

@timgraham timgraham closed this Feb 7, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment