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

标签 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)

我需要做的是获取两家公司的所有公司评级选项,但不要让它们在指标上重叠 (rating.rating_option.indicator)。如果有冲突,公司“a”总是会胜过公司“b”。我该怎么做?

最佳答案

这个有效:

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

(原始答案)

你试过吗

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

?

关于python - 在 Django 中,我有一个复杂的查询,我只需要通过外键获取唯一值,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3323591/

相关文章:

python - 根据输入比例更新 Pandas 数据框中一列的值

python - 使用 Scikit-learn 中的 Column Transformer 时如何找出 StandardScaling 参数 .mean_ 和 .scale_?

python - python对象的自动redis查找

mysql - Python3 中的 Django 数据库

python - “不同”条目仅用于使用 Django 创建 sitemap.xml 文件 (1.5.4)

django - django 如何在 postgresql 中的 text[] 之后建模

python - 旋转 SQLite 表,像 SQL 一样应该是

django - 如何在 Django 的终端后台启动 celery

django - 我是否在 django 中以错误的方式重写了模型上的保存方法?

django - 在django中保存后对象的id为none