How retuen all rows of a group item in DJANGO?

My data base is real estated related and I have history of transaction of properties. I have this code:

` def filter_model(estate, years=[], exact_match=False, distance_km=None): query_filter = Q(location__postal_code__isnull=False, sold_histories__isnull=False)

        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()
        ]`

I need to optimize this code, the reason I made the list is that I need to return all history of an address from the database. I am using this code to get data,

km = 1 * 1000 estates_by_km.extend(filter_model(estate=estate, distance_km=km//2))

If I don't return the list it would only return the last history per each address. Is there anyway to return all history of filtered address without making a list?

This code works correct I only need to optimize it.

Is making the list a significant overhead compared to the database query?

If you want to process itews from the queryset one by one you might turn this function into a Python generator function. The last part would look like

for e in estates:
    for sh in e.sold_histories.all()
       yield [
          e.id,
          ...
          sh.price
       ]

and the calling code would be

for item in filter_model( estate, other_args...):
    # do something with item, which is a simple list as yield'ed.
    e_id = item[0]
    ...

I would either yield a dict rather than a list, or I would define symbolic names for the indexes into the list ( E_ID=0, E_BEDROOMS = 1, ... etc ) both of which remove a lot of scope for errors. ( For the OP I expect this is teaching grandma to suck eggs, but others may read this).

Back to Top