danceschool_tags.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:django-danceschool 作者: django-danceschool 项目源码 文件源码
def get_item_by_key(passed_list, key, value):
    '''
    This one allows us to get one or more items from a list of
    dictionaries based on the value of a specified key, where
    both the key and the value can be variable names.  Does
    not work with None or null string passed values.
    '''

    if value in [None,'']:
        return

    if type(passed_list) in [QuerySet, PolymorphicQuerySet]:
        sub_list = passed_list.filter(**{key: value})
    else:
        sub_list = [x for x in passed_list if x.get(key) == value]

    if len(sub_list) == 1:
        return sub_list[0]
    return sub_list
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号