Skip to content

Add filtering by list to Query.filter #11

@enlike

Description

@enlike

Hi!

I've faced some issue with list filter and find some improvement that can help filter by list

class Query...
     # your solution
    def filter(self, value) -> "Query[Q]":
        """
        Set ``$filter`` query parameter. Can be called multiple times. Multiple
        :py:func:`filter` calls are concatenated with 'and'

        :param value: Property comparison. For example, ``Entity.Property == 2``
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$filter')
        option.append(value)
        return q

Improvement to extend list filter

class Query...
     # my solution
    def filter(self, value) -> "Query[Q]":
        """
        Set ``$filter`` query parameter. Can be called multiple times. Multiple
        :py:func:`filter` calls are concatenated with 'and'

        :param value: Property comparison. For example, ``Entity.Property == 2``
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$filter')
        if isinstance(value, list):
            option.extend(value)
        else:
            option.append(value)
        return q

e.g.

entity = entities[my_entity]
q = query(entity)
exclude_sids = ['6f98ba36-3b7f-4767-8369-88a65578dc5a', '5afa06fb-3b66-4216-8681-56acdeac7fc1']
sid_filters = [group_entity.Sid != i for i in exclude_sids]
# filters by odata filter[Sid ne 6f98ba36-3b7f-4767-8369-88a65578dc5a, Sid ne 5afa06fb-3b66-4216-8681-56acdeac7fc1]
q = q.filter(sid_filters).all()
# output log  
# Query: {'$filter': '(Sid ne 6f98ba36-3b7f-4767-8369-88a65578dc5a) and (Sid ne 5afa06fb-3b66-4216-8681-56acdeac7fc1)

Kind regard, Pavel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions