views:

32

answers:

1

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 of the company rating options of two companies without having them overlap on their Indicators (rating.rating_option.indicator). If there's a conflict, company 'a' would always win over company 'b'. How do I do this?

A: 

This works:

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

(Original answer)

Did you try

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

?

godswearhats
IndicatorRatingOption isn't related to company that way.
After reviewing, you were off by one. You meant: Rating.filter(company__in=[company_a, company_b]).distinct()
Cool. I kinda threw it together off the top of my head. I knew the distinct() was the key part. I've edited my original answer to show what you've discovered.
godswearhats