[Answer]-Django method same object not saving

1đź‘Ť

âś…

Thank you pztrick for the hint with the caches. I finally solved it. So the problem was that I was doing:

x = get_object_or_404(BaseItem, ppk=sou.ppk, project=sou.project, current=True)
createRevision(request, "", x)
# .... loads of lines of code
unlock(x)

unlock is a method I wrote that just sets a timestamp so I know no other user is editing it. So now the problem was that I was saving x in createRevision with all the correct data but of course unlock(x) still had a reference to an “old” not updated object and of course was saving it again. Hence it was overwriting my changes in createRevision.

Thank you again to everyone who helped with this.

👤Didi

0đź‘Ť

I think you may be running afoul of model manager caching which is intended to limit database queries. However, by invoking the .all() method on the model manager you force it to hit the databse again.

So, try this: Replace your argument from the BaseItem class to the model manager’s .all() QuerySet:

source = get_object_or_404(BaseItem.objects.all(), ppk=sou.ppk, project=sou.project, current=True)
# ...
rofl =  get_object_or_404(BaseItem.objects.all(), pk=source.pk, project=sou.project)

get_object_or_404 supports mode classes, model managers, or QuerySets as the first parameter so this is valid.

👤pztrick

Leave a comment