在Django我有一个复杂的查询,我只需要通过外键的唯一值,这可能吗?

dam*_*mon 5 python django django-models

我有以下型号:

class Indicator(models.Model):
    name = models.CharField(max_length=200)
    category = models.ForeignKey(IndicatorCategory)
    weight = models.IntegerField()
    industry = models.ForeignKey(Industry)

    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ('name',)

class IndicatorRatingOption(models.Model):
    indicator = models.ForeignKey(Indicator)
    description = models.TextField()
    value = models.FloatField(null=True)

    def __unicode__(self):
        return self.description

class Rating(models.Model):
    product = models.ForeignKey(Product, null=True)
    company = models.ForeignKey(Company, null=True)
    rating_option = models.ForeignKey(IndicatorRatingOption)
    value = models.IntegerField(null=True)
Run Code Online (Sandbox Code Playgroud)

我需要做的是获得两家公司的所有公司评级选项,而不会在指标(rating.rating_option.indicator)上重叠.如果存在冲突,公司'a'将永远战胜公司'b'.我该怎么做呢?

god*_*ats 5

这有效:

Rating.objects.filter(company__in=[company_a, company_b]).distinct()
Run Code Online (Sandbox Code Playgroud)

(原始答案)

你试过了吗

IndicatorRatingOptions.objects.filter(company__in=[company_a, company_b]).distinct()
Run Code Online (Sandbox Code Playgroud)