Opened 16 years ago
Closed 16 years ago
#12709 closed (duplicate)
Refetch method for getting a fresh model instance
| Reported by: | Jukka Välimaa | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | |
| Severity: | Keywords: | refetch | |
| Cc: | Jukka Välimaa | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
It is often useful to get a fresh instance of a model object, with data from db. A typical case is testing some view code using the test client, when the
view is supposed to save some model changes into database. For example:
client.post('some_url', {'id': obj.id, 'description': 'new'})
fresh_obj = ObjClass.objects.get(pk=obj.pk)
self.assertEqual('new', fresh_obj.description)
This ticket proposes adding a method to Model that can be used to fetch a "fresh" version of a model object from the database. It is functionally the same as the use of objects.get in the example above, but more convenient and elegant.
fresh_obj = obj.refetch()
Note:
See TracTickets
for help on using tickets.
Originally proposed in django-developers list here:http://groups.google.com/group/django-developers/browse_thread/thread/6129eccb0a9b93d8.