复杂的查询数据库

时间:2019-09-04 14:06:05

标签: django django-models

请参见以下情况:

class Case(models.Model):

    name = models.CharField(max_length=200)
    documentation = models.CharField(max_length=2048, blank=True)


class Suite(models.Model):

    name = models.CharField(max_length=200)
    documentation = models.CharField(max_length=2048, blank=True)
    cases = models.ManyToManyField(Case, through='SuiteThroughModel')



class Project(models.Model):

    name = models.CharField(max_length=200)
    documentation = models.CharField(max_length=2048, blank=True)
    suites = models.ManyToManyField(Suite, through='ProjectSuiteThroughModel')


class ProjectSuiteThroughModel(OrderedModel):

    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    suite = models.ForeignKey(Suite, on_delete=models.CASCADE)


class SuiteThroughModel(OrderedModel):

    case = models.ForeignKey(Case, on_delete=models.CASCADE)
    suite = models.ForeignKey(Suite, on_delete=models.CASCADE)

我要尝试的是从特定项目中检索所有套件。 这是我的处理方式:

project = Project.objects.get(name='xyz')
suites = project.suites.all() # outputs nothing

在Django文档中,我了解到可以从查询的另一端获取数据:

suites = Suite.objects.filter(project__name='xyz') # outputs nothing
suites = Suite.objects.filter(projectsuitethroughmodel__project__name='xyz') # outputs nothing

数据在那里,但是我该如何访问呢?

1 个答案:

答案 0 :(得分:-1)

正如@WillemVanOnsem和@luxcem指出的那样。我需要显式链接表。