問題の背景:
ORMは、次のような複数のオブジェクトのリストをチェックしてください
list1 = list(Adgroup.objects.filter(shop_id=shop_id,adgroup_id__in=adgroup_id_list1))
list2 = list(Adgroup.objects.filter(shop_id=shop_id,adgroup_id__in=adgroup_id_list2))
list3 = list(Adgroup.objects.filter(shop_id=shop_id,adgroup_id__in=adgroup_id_list3))
list4 = list(Adgroup.objects.filter(shop_id=shop_id,adgroup_id__in=adgroup_id_list4))
要件:
再集合およびオブジェクトリスト内のオブジェクトの複数の交点に必要。
この上に?
直に
real_obj_list = list(set(list1).union(list2, list3, list4)) # 求多个list的并集
直接エラー:広告グループクラスオブジェクトはハッシュではありません!
ここにフォーカス!
注:クラスオブジェクトの重みに設定され、前記方法オーバーライド__eq__ __hash__メソッド、クラスオブジェクトの__hash__原因広告グループのハッシュを書き換えていない場合ではありません
class Adgroup(object):
def __init__(self, title, price):
self.title= title
self.price= price
def __eq__(self, other):
if isinstance(other, Adgroup):
# title和price相同就认为对象相同
return ((self.title== other.title) and (self.price== other.price))
else:
return False
def __ne__(self, other):
return (not self.__eq__(other))
def __hash__(self):
# title和price相同就认为对象相同
return hash(self.title) + hash(self.price)
その後、再それになるオブジェクトリストにセット()オブジェクトを使用することができます!!!