Hi Tim,
Thanks for the reply! If I am understanding it correctly I don’t think this is the issue as I am creating the ModelB instance in a function on ModelA (using self
as the foreign key) and then further down the same function calling another function on self
that accesses the _set
. Here is a clearer demonstration of how it is structured in my code:
class ModelA:
def fn():
# ...
b = ModelB(a=self, text="test")
b.save()
# self.refresh_from_db() # fixes the issue
# ...
self.otherFn()
# ...
def otherFn():
# ...
self.modelb_set.all() # Outdated results
# ModelB.objects.filter(modela=self) # Up to date results
# ...
The first time it works perfectly (presumably because there is no cache), but if I run fn()
a second time it will only give the content added the first time, and a third time will give the contents from the first and second time and so on…
Or am I misunderstanding and self
(a result from a query) due to caching is an instance in time and a new instance needs to be created, or it needs to be reloaded from the database, before any changes can be seen on self
(although testing this concept in a shell on a demo django project an that doesn’t seem to replicate the issue).