def getpid(key): userid = '113073984862808105932' try: print('key', key) emo = Photo.objects(Q(userId=userid) & Q(tag__emotion_tag=key)) print('emotion:', emo) addpid(emo, key) main = Photo.objects(Q(userId=userid) & Q(tag__main_tag=key)) print('main:', main) addpid(main, key) top3 = Photo.objects(Q(userId=userid) & Q(tag__top3_tag__tag=key)) print('top3:', top3) addpid(top3, key) alltag = Photo.objects(Q(userId=userid) & Q(tag__all_tag__tag=key)) print('alltag:', alltag) addpid(alltag, key) custom = Photo.objects( Q(userId=userid) & Q(tag__custom_tag__is_deleted=False) & Q(tag__custom_tag__tag=key)) print('custom:', custom) addpid(custom, key) location = Photo.objects(Q(userId=userid) & Q(location=key)) print('location:', location) addpid(location, key) album = Album.objects( Q(userId=userid) & Q(albumTag__isDeleted=False) & Q(albumTag__tag=key)) # print('album:',album) for i in album: photos = i.albumPhoto # print('photos:',photos) for j in photos: atag = [] isdeleted = j.isDeleted if isdeleted is False: pid.append(j.photoId) atag.append(key) atemptag = atag pid_tag.append({"pid": j.photoId, "tag": atemptag}) print('pid_tag', pid_tag) except Exception as e: print(e)