In Django, I have a complex query where I only need unique values ​​through a foreign key, is this possible?

I have the following 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)

What I need to do is get all the rating options for the company of the two companies without overlaying them on my indicators (rating.rating_option.indicator). If there is a conflict, company a will always defeat company b. How to do it?

+5
source share
1 answer

It works:

Rating.filter(company__in=[company_a, company_b]).distinct()

(Original answer)

You tried

IndicatorRatingOptions.filter(company__in=[company_a, company_b]).distinct()

?

+3
source

All Articles