Add a way to control related fields reverse cache
|Reported by:||dirleyrls||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Severity:||Normal||Keywords:||orm realted cache|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I have a very specific use-case where we use a database view to represent some data. I've mapeed it using a unmanaged model like this:
# concrete table class Something(models.Model): # ... class SomethingHelperView(models.Model): something = models.OneToOneField(Something, related_name='helper') counter = models.PositiveIntegerField() class Meta: managed = False db_table = 'something_helper_view'
When I do:
>>> something = Something.objects.get(pk=1) >>> something.helper.counter 10 >>> # some operation that will update the counter >>> # ... >>> something.helper.counter 10
This was not the behavior I wanted to have. Since I'm using a database view, I expect it to always be "fresh". I want that, every time I access my helper, it hits the database, bringing me a fresh value. That's the point of this view.
Poking around through Django's code, I realized that related fields have their back references cached. It's ok, I understand the purpose of that cache. But I got frustrated when I discovered I can't just turn it off. It would be good to be able to turn it off.
And for the record, currently I'm working around this issue doing like this:
Something.fresh_helper = property(lambda self: SomethingHelperView.get(something=self))
Change History (5)
comment:1 follow-up: ↓ 2 Changed 3 years ago by aaugustin
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Resolution set to needsinfo
- Status changed from new to closed
comment:3 Changed 3 years ago by aaugustin
- Resolution needsinfo deleted
- Status changed from closed to reopened