开发者

Foreign Key relations in Django, with a select condition

I've a table of partner type

PartnerType
Name
Description
RelatedToProject

I've a PartnerMaster

Name
Address
PartnerType

I've a Project Table

Name
Partner (from partner master)

The partner in the project table has to be only of those types who have RelatedToProject = True

How can I achieve this in the model definition itself开发者_StackOverflow.


I don't have much experience with Django, but you may want to consider removing the RelatedToProject field and adding another class called PartnersRelatedToProjects (or something like that). Then simply set up a normal foreign key from this new class to the Partner table, and in the Project class set up a normal foreign key to this new table.

You would then need to keep track of which partners are related to projects by adding them to the new PartnersRelatedToProjects table.

class Partner(models.Model):
    Name = models.CharField(max_length=200)
    Description = models.CharField(max_length=200)

class PartnersRelatedToProjects(models.Model):
    partner = models.ForeignKey('Partner')

class Project(models.Model):
    name = models.CharField(max_length=200)
    partner = models.ForeignKey('PartnersRelatedToProjects')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜