Django模型聚合匹配ManyToMany中的计数

时间:2016-05-11 05:46:04

标签: django django-models django-orm django-aggregation

Model with ManyToMany字段:

if (!$this->isValidForSend()) {
    $_helper->log('Email is not valid for sending, this is a core error that often means there\'s a problem with your email templates.');
    Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
    return false; }

例如,我们有这样的数据集:

semanage permissive -a nagios_t

我正在寻找一种方法来聚合相同数量的MyModel。

f.e。 MyModel对象在其number_set中至少有3个相同的数字。

class Number(Model):
    current_number = IntegerField()

class MyModel(models.Model):
    numbers_set = models.ManyToMany(Number)

1 个答案:

答案 0 :(得分:0)

如果您使用的是Postgres版本9.4和Django版本1.9,最好使用JSONField()而不是ManyToMany(),以便在Postgres上使用jsonb索引进行索引编制将为您提供有效的查询数据。查看here