通过查询值列表进行查询集过滤

时间:2019-06-05 17:27:22

标签: django django-rest-framework

django初学者的问题...

这是JSON响应...

"data": [
{
    "type": "Subject",
    "id": "0",
    "attributes": {
        "created": "2019-01-01T00:00:00Z",
        "modified": "2019-01-01T00:00:00Z",
        "subject_code": "A&H",
        "subject_name": "AH",
        "subject_short_name": "A & HUM"
    },
    "relationships": {
        "organization": {
            "data": {
                "type": "Organization",
                "id": "61"
            }
        },
        "created_user": {
            "data": null
        },
        "last_updated_user": {
            "data": null
        }
    },
    "links": {
        "self": "http://localhost:8001/v1/subject_owner/0"
    }
},

以上响应来自串行器

queryset = Subject.objects.all()

我有一个查询

http://localhost:8001/v1/subject_owner?owner_ids=62,63

那么,如何将owner_ids的过滤条件写为列表?响应中应仅包含owner_ids与organization_id相匹配的结果。我尝试过几次:

queryset.filter(organization__in=[owner_id_list])

queryset.filter(organization=owner_id_list)

显然,它们不起作用。任何帮助将不胜感激。

仅供参考,这是模型课...

class SubjectManager(models.Manager):

    def get_by_natural_key(self, subject_code):
        return self.get(subject_code=subject_code)


class Subject(get_subject_base_class(Organization)):

    objects = SubjectManager()

    def __str__(self):
        return self.subject_code

    def natural_key(self):
        return (self.subject_code,)

1 个答案:

答案 0 :(得分:0)

您必须做类似的事情:

owner_id_list = [62, 63]
Subject.objects.filter(organization_id__in=owner_id_list)