def unique_for_country_code(self, country_code):
shipping = self.filter(
Q(country_code=country_code) |
Q(country_code=ANY_COUNTRY))
shipping = shipping.order_by('shipping_method_id')
shipping = shipping.values_list('shipping_method_id', 'id', 'country_code')
grouped_shipping = groupby(shipping, itemgetter(0))
any_country = ANY_COUNTRY
ids = []
for shipping_method_id, method_values in grouped_shipping:
method_values = list(method_values)
# if there is any country choice and specific one remove any country choice
if len(method_values) == 2:
method = [val for val in method_values if val[2] != any_country][0]
else:
method = method_values[0]
ids.append(method[1])
return self.filter(id__in=ids)
评论列表
文章目录