filter qoshildi

This commit is contained in:
behruz-dev
2025-11-29 17:28:58 +05:00
parent 68c2c442fb
commit 4d9d0606f5
2 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,49 @@
# django
from django.db import transaction
# rest framework
from rest_framework import serializers
# shared
from core.apps.shared.models import Location, UserLocation, District, Place, Doctor, Pharmacy
class LocationListSerializer(serializers.ModelSerializer):
district = serializers.SerializerMethodField(method_name='get_district')
place = serializers.SerializerMethodField(method_name='get_place')
doctor = serializers.SerializerMethodField(method_name='get_doctor')
pharmacy = serializers.SerializerMethodField(method_name='get_pharmacy')
class Meta:
model = Location
fields = [
'id', 'longitude', 'latitude', 'created_at',
'district', 'place', 'doctor', 'pharmacy',
]
def get_district(self, obj):
return {
'id': obj.district.id,
'name': obj.district.name,
} if obj.district else None
def get_place(self, obj):
return {
'id': obj.place.id,
'name': obj.place.name,
} if obj.place else None
def get_doctor(self, obj):
return {
'id': obj.doctor.id,
'first_name': obj.doctor.first_name,
'last_name': obj.doctor.last_name,
} if obj.doctor else None
def get_pharmacy(self, obj):
return {
'id': obj.pharmacy.id,
'name': obj.pharmacy.name,
} if obj.pharmacy else None

View File

@@ -31,6 +31,13 @@ class DistrictListApiView(generics.GenericAPIView, ResponseMixin):
name='name', name='name',
description="tuman nomi bo'yicha qidiruv", description="tuman nomi bo'yicha qidiruv",
required=False, required=False,
),
openapi.Parameter(
in_=openapi.IN_QUERY,
type=openapi.TYPE_INTEGER,
name='user',
description="user id bo'yicha filter",
required=False,
) )
], ],
responses={ responses={
@@ -96,11 +103,13 @@ class DistrictListApiView(generics.GenericAPIView, ResponseMixin):
def get(self, request): def get(self, request):
try: try:
name = request.query_params.get('name', None) name = request.query_params.get('name', None)
user_id = request.query_params.get('user', None)
query = self.queryset.all() query = self.queryset.all()
if not name is None: if not name is None:
query = query.filter(name__istartswith=name) query = query.filter(name__istartswith=name)
if not user_id is None:
query = query.filter(user__id=user_id)
page = self.paginate_queryset(queryset=query) page = self.paginate_queryset(queryset=query)
if page is not None: if page is not None:
serializer = self.serializer_class(page, many=True) serializer = self.serializer_class(page, many=True)