Django set model attribute to another model definition
I have a pair of models which I want to reference one another, but not as a fore开发者_开发问答ign key. Ideally, I want this reference just to be an attribute the model. But the problem is that the second model wont have been created for the first to reference.
Example:
class model1(models.Model):
...
relatedModel = model2
class model2(models.Model):
...
relatedModel = model1
A similar thing happens when a foreign key is created using double quotes such as
field = models.foreignKey('someModel')
But I dont want this relationship to be a foreignkey...
I hope this makes sense, thanks
Class attributes don't always need to be set when the class is defined, certainly not in this case:
class model1(models.Model):
...
class model2(models.Model):
...
relatedModel = model1
model1.relatedModel = model2
精彩评论