diff --git a/app/__pycache__/helper.cpython-39.pyc b/app/__pycache__/helper.cpython-39.pyc index 43376aa..e96dcca 100644 Binary files a/app/__pycache__/helper.cpython-39.pyc and b/app/__pycache__/helper.cpython-39.pyc differ diff --git a/app/__pycache__/models.cpython-39.pyc b/app/__pycache__/models.cpython-39.pyc index b18875b..66c6394 100644 Binary files a/app/__pycache__/models.cpython-39.pyc and b/app/__pycache__/models.cpython-39.pyc differ diff --git a/app/__pycache__/serializers.cpython-39.pyc b/app/__pycache__/serializers.cpython-39.pyc index 98ff34d..cbc0836 100644 Binary files a/app/__pycache__/serializers.cpython-39.pyc and b/app/__pycache__/serializers.cpython-39.pyc differ diff --git a/app/__pycache__/views.cpython-39.pyc b/app/__pycache__/views.cpython-39.pyc index 482277c..45e4059 100644 Binary files a/app/__pycache__/views.cpython-39.pyc and b/app/__pycache__/views.cpython-39.pyc differ diff --git a/app/migrations/0048_allproductstransport_app_allprod_date_e7e7e8_idx.py b/app/migrations/0048_allproductstransport_app_allprod_date_e7e7e8_idx.py new file mode 100644 index 0000000..7d0fd69 --- /dev/null +++ b/app/migrations/0048_allproductstransport_app_allprod_date_e7e7e8_idx.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.19 on 2026-02-10 12:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0047_hatching_loadingsum'), + ] + + operations = [ + migrations.AddIndex( + model_name='allproductstransport', + index=models.Index(fields=['date', 'id'], name='app_allprod_date_e7e7e8_idx'), + ), + ] diff --git a/app/migrations/__pycache__/0048_allproductstransport_app_allprod_date_e7e7e8_idx.cpython-39.pyc b/app/migrations/__pycache__/0048_allproductstransport_app_allprod_date_e7e7e8_idx.cpython-39.pyc new file mode 100644 index 0000000..beae93a Binary files /dev/null and b/app/migrations/__pycache__/0048_allproductstransport_app_allprod_date_e7e7e8_idx.cpython-39.pyc differ diff --git a/app/migrations/__pycache__/0048_allproductstransport_indexes.cpython-39.pyc b/app/migrations/__pycache__/0048_allproductstransport_indexes.cpython-39.pyc new file mode 100644 index 0000000..049c938 Binary files /dev/null and b/app/migrations/__pycache__/0048_allproductstransport_indexes.cpython-39.pyc differ diff --git a/app/models.py b/app/models.py index 9ba98d2..7d9caff 100644 --- a/app/models.py +++ b/app/models.py @@ -1,4 +1,5 @@ import datetime +from operator import index import re from datetime import timedelta from itertools import product @@ -671,6 +672,11 @@ class AllProductsTransport(BaseModel): # بارهای تمامی محصولات unloading = models.CharField(max_length=255, null=True, blank=True) # تخلیه out = models.BooleanField(default=False) # Out (True/False) + class Meta: + indexes = [ + models.Index(fields=['trash', '-date', '-id'], name='apt_trash_date_id_idx'), + ] + def save(self, *args, **kwargs): # تصحیح استان/شهر مبدا if self.origin_province: diff --git a/app/serializers.py b/app/serializers.py index 062e2f0..59d78e6 100644 --- a/app/serializers.py +++ b/app/serializers.py @@ -622,66 +622,32 @@ class AllProductsTransportCustomSerializer(serializers.ModelSerializer): def get_location_origin(self, obj): resul = {} - # بهینه‌سازی: استفاده از context cache برای جلوگیری از N+1 queries - location_cache = self.context.get('location_cache', {}) - - if obj.origin_province: - cache_key = f"province_{obj.origin_province}" - if cache_key not in location_cache: - province_origin = Province.objects.filter(name=obj.origin_province).only('Lat', 'Lng').first() - if province_origin: - location_cache[cache_key] = { - "provinceLat": province_origin.Lat, - "provinceLng": province_origin.Lng - } - else: - location_cache[cache_key] = {} - resul.update(location_cache.get(cache_key, {})) - - if obj.origin_city: - cache_key = f"city_{obj.origin_city}" - if cache_key not in location_cache: - city_origin = City.objects.filter(name=obj.origin_city).only('Lat', 'Lng').first() - if city_origin: - location_cache[cache_key] = { - "cityLat": city_origin.Lat, - "cityLng": city_origin.Lng - } - else: - location_cache[cache_key] = {} - resul.update(location_cache.get(cache_key, {})) - + province_origin = Province.objects.filter(name=obj.origin_province).first() + city_origin = City.objects.filter(name=obj.origin_city).first() + if province_origin: + resul.update({ + "provinceLat": province_origin.Lat, + "provinceLng": province_origin.Lng + }) + if city_origin: + resul.update({ + "cityLat": city_origin.Lat, + "cityLng": city_origin.Lng + }) return resul def get_location_destination(self, obj): resul = {} - # بهینه‌سازی: استفاده از context cache برای جلوگیری از N+1 queries - location_cache = self.context.get('location_cache', {}) - - if obj.destination_province: - cache_key = f"province_{obj.destination_province}" - if cache_key not in location_cache: - province_destination = Province.objects.filter(name=obj.destination_province).only('Lat', 'Lng').first() - if province_destination: - location_cache[cache_key] = { - "provinceLat": province_destination.Lat, - "provinceLng": province_destination.Lng - } - else: - location_cache[cache_key] = {} - resul.update(location_cache.get(cache_key, {})) - - if obj.destination_city: - cache_key = f"city_{obj.destination_city}" - if cache_key not in location_cache: - city_destination = City.objects.filter(name=obj.destination_city).only('Lat', 'Lng').first() - if city_destination: - location_cache[cache_key] = { - "cityLat": city_destination.Lat, - "cityLng": city_destination.Lng - } - else: - location_cache[cache_key] = {} - resul.update(location_cache.get(cache_key, {})) - + province_destination = Province.objects.filter(name=obj.destination_province).first() + city_destination = City.objects.filter(name=obj.destination_city).first() + if province_destination: + resul.update({ + "provinceLat": province_destination.Lat, + "provinceLng": province_destination.Lng + }) + if city_destination: + resul.update({ + "cityLat": city_destination.Lat, + "cityLng": city_destination.Lng + }) return resul diff --git a/app/views.py b/app/views.py index 8df65a0..5b27782 100644 --- a/app/views.py +++ b/app/views.py @@ -4629,15 +4629,14 @@ class AllProductsTransportViewSet(viewsets.ModelViewSet): # بهینه‌سازی: استفاده از select_related برای جلوگیری از N+1 queries # و اعمال order_by فقط یک بار - transports = AllProductsTransport.objects.select_related('hatching').filter(**filters) + transports = AllProductsTransport.objects.filter(**filters) if search and search != 'undefined' and search.strip(): transports = transports.filter( build_query(self.filterset_class.Meta.fields, search) ) - # اعمال order_by فقط یک بار در آخر - transports = transports.order_by('-date', '-create_date') + transports = transports.order_by('-date', '-id') page_size = request.query_params.get('page_size', None) if page_size: