Моя база данных связана с недвижимостью, и у меня есть история транзакций с недвижимостью. У меня есть этот code:
` def filter_model(estate, years=[], exact_match=False, distance_km=None): query_filter = Q(location__postal_code__isnull=False, sold_histories__isnull=False)
Мне нужно оптимизировать этот code, причина, по которой я создал список, заключается в том, что мне нужно вернуть всю историю адреса из базы данных. Я использую этот code для получения данных,
km = 1 * 1000 estates_by_km.extend(filter_model(estate=estate, distance_km=km//2))
Если я не верну список, он вернет только последнюю историю для каждого адреса. Можно ли как-то вернуть всю историю отфильтрованных адресов, не создавая список?
Этот code работает правильно, мне нужно только оптимизировать его.
if distance_km:
query_filter &= Q(location__point__distance_lte=(
estate.location.point, distance_km))
query_filter &= Q(type=estate.type)
query_filter &= Q(square_feet__isnull=False) # >1500
query_filter &= Q(year_built__isnull=False)
if estate.type == 2:
address = str(estate.location.address).split('#')[0].strip()
query_filter &= Q(location__address__icontains=address)
estates = Estate.objects.prefetch_related(
# ? This is for speed up loop.
'location__city',
'location__neighbourhood',
Prefetch('sold_histories', queryset=SoldHistory.objects.order_by('-date'))
).filter(query_filter).distinct()
else:
if len(years) == 2:
query_filter &= Q(sold_histories__date__year=years[0])
if exact_match:
query_filter &= Q(location__city__province__id=estate.location.city.province.id)
else:
query_filter &= Q(location__city__id=estate.location.city.id)
estates = Estate.objects.prefetch_related(
# ? This is for speed up loop.
'location__city',
'location__neighbourhood',
Prefetch('sold_histories', queryset=SoldHistory.objects.filter(date__year__in=years))
).filter(query_filter).filter(sold_histories__date__year=years[1]).distinct()
return [
[
e.id,
e.bedrooms,
e.type,
e.year_built,
e.square_feet,
e.lot_size,
e.location.id,
e.location.address,
e.location.postal_code,
e.location.latitude,
e.location.longitude,
e.location.city.name if e.location.city else None,
e.location.neighbourhood.name if e.location.neighbourhood else None,
sh.date,
sh.price
]
for e in estates
for sh in e.sold_histories.all()
]`