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
danceschool_tags.py 文件源码
python
阅读 23
收藏 0
点赞 0
评论 0
评论列表
文章目录