开发者

Django set model attribute to another model definition

开发者 https://www.devze.com 2023-02-09 08:16 出处:网络
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 se

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
0

精彩评论

暂无评论...
验证码 换一张
取 消