I have the following model :
class A:
b = ForeignKey('B')
class B:
a = ForeignKey('A', null=True)
I have several methods that decode a json object and create a tree of objects A and B.
Suppose I have an object of type A that I want to save, I’m trying to write something like this
in class A :
def save(self, *args, **kwargs):
self.b.save()
super(A, self).save(*args, **kwargs)
in B :
def save(self, *args, **kwargs):
if self.a:
self.a.save()
super(B, self).save(*args, **kwargs)
This does not work, even though A.b and B.a get assigned an id while calling save(), it still violates the not null constraint in A.
I’ve read somewhere that this was due to how ORM works, and how the objects are cached somehow.
The solution proposed was to do something like this :
a = A()
b = B()
b.save()
a.b = b
a.save()
But for obvious reason of recursion, it’s not appropriate in my case. So the only work around I can think of is to provide each objects with a method that recursively retrieve every object that need to be saved, and then do a for loop to save each one in the right order. I really would like to avoid this, since of course the actual model is more complex and involves more than two classes and more than one Foreign Key per class.
So I guess my question is simply : Is there a better way or a more common way to proceed in such situations ?
Well after some soul searching I found out that this was indeed a cache “problem”. I don’t have the time to look inside django code to understand how things really work, so if anyone have some interesting insights that would be great.
In the meantime, the solution I found is to force the object to clear its cache like this :
This does work, but for the record here is a little helper to automatically clear cache before any saving :