首页 > 解决方案 > django 嵌套查询过滤

问题描述

我有嵌套模型。像这些:

class Items_Relation_Income_Statement(models.Model):
    statement = models.ForeignKey('Items_Income_Statement', related_name="items" ,on_delete = models.CASCADE )  
    item = models.ForeignKey('Income_Statement' ,on_delete = models.CASCADE ) 
    amount = models.BigIntegerField()

class Items_Income_Statement(models.Model):
    statement_id = models.BigIntegerField(primary_key=True)
    com_id = models.IntegerField()
    bourse_symbol = models.CharField(max_length= 100, null=True, blank=True)
    full_title = models.CharField(max_length= 100, null=True, blank=True)
    period_type = models.IntegerField()
    fiscal_year_end = models.CharField(max_length= 100, null=True, blank=True)
    jalali_fiscal_year_end = models.CharField(max_length= 100, null=True, blank=True)
    period_end = models.CharField(max_length= 100, null=True, blank=True)
    jalali_period_end = models.CharField(max_length= 100, null=True, blank=True)
    anouncement_date = models.CharField(max_length= 100, null=True, blank=True)
    jalali_anouncement_date = models.CharField(max_length= 100, null=True, blank=True)
    audited = models.BooleanField()
    represented = models.BooleanField()
    consolidated = models.BooleanField()

查询字符串的结果是这样的:

 {
        "statement_id": 129928,
        "com_id": 1,
        "bourse_symbol": "rrr",
        "full_title": "ghg",
        "period_type": 12,
        "fiscal_year_end": "2012-03-19T00:00:00",
        "jalali_fiscal_year_end": "1390/12/29",
        "period_end": "2012-03-19T00:00:00",
        "jalali_period_end": "1390/12/29",
        "anouncement_date": "2013-06-11T00:00:00",
        "jalali_anouncement_date": "1392/03/21",
        "audited": true,
        "represented": true,
        "consolidated": false,
        "items": [
            {
                "id": 73,
                "amount": -272087,
                "statement": 129928,
                "item": 1
            },
            {
                "id": 74,
                "amount": -8075,
                "statement": 129928,
                "item": 5
            },
]
}

我只想拥有一些具有特殊 item_id 的项目,而不是全部。我应该怎么办?有什么方法可以在项目列表中获取特殊项目而不是全部

标签: djangopostgresqldjango-queryset

解决方案


推荐阅读