views:

27

answers:

1

Lets assume I had the following Model

class A(Models.model):
   def __init__(self,data):
      B(a=self,data=data).save()

class B(Models.model):
   data = somefieldtype
   a = Models.models.ForeignKey('A')

now as you might suspect, there is an error in this Model definintion, as one cannot create a relation to the A instance before ainstance.save() has been called. However, this type of init method would make my controllers much simpler. Is there a way to avoid this problem?

+2  A: 

You can put this code in an overridden save method of A:

def save(self,**kwargs):
   super(A,self).save(**kwargs)
   B(a=self,data=data).save()
adamk