How retuen all rows of a group item in DJANGO?

  Kiến thức lập trình

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.

New contributor

user9272398 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

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.

New contributor

user9272398 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT