Advice on designing django model with "exclusive" foreign key relationships
I want to design a Task model that can be associated to Project Models, another X model, and other Task models. The obvious choice is 开发者_如何学Pythona foreign key, but I want any particular instance of that Task model to only be associated to only one of those model types, that is, if a Task model has a relationship with a Project model, it cannot have a relationship with another Task model, and so on. Any advice in what would be the best way to represent this? Thanks.
Have a look at Generic relation. It lets you define a foreign key on multiple models. This way your task is only linked to one of your models.
What I have done is to inherit from a base class on all my models that will be related to tasks. Task models points to that base class on the ForeignKey with unique=True, and it seems like all subclasses inherit the relationship. Thanks.
精彩评论