best deal price fields added to product serialzier

This commit is contained in:
Parsa Nazer
2025-12-26 11:08:32 +03:30
parent 4223461c2b
commit 52a997ec0b
2 changed files with 27 additions and 3 deletions
+3
View File
@@ -210,6 +210,9 @@ class ProductModel(models.Model):
if not self.slug: if not self.slug:
self.slug = slugify(self.name, allow_unicode=True) self.slug = slugify(self.name, allow_unicode=True)
super().save(*args, **kwargs) super().save(*args, **kwargs)
def get_best_deal_variant(self):
return self.variants.order_by("price", "-discount").first()
class Meta: class Meta:
verbose_name = 'محصول' verbose_name = 'محصول'
+24 -3
View File
@@ -136,7 +136,9 @@ class DynamicProductSerializer(serializers.ModelSerializer):
is_new = serializers.SerializerMethodField() is_new = serializers.SerializerMethodField()
related_products = serializers.SerializerMethodField() related_products = serializers.SerializerMethodField()
added_to_favorites = serializers.SerializerMethodField() added_to_favorites = serializers.SerializerMethodField()
best_deal_price_before_discount = serializers.SerializerMethodField()
best_deal_price_after_discount = serializers.SerializerMethodField()
best_deal_discount = serializers.SerializerMethodField()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
view_type = self.context.get('view_type', 'all') view_type = self.context.get('view_type', 'all')
@@ -152,11 +154,30 @@ class DynamicProductSerializer(serializers.ModelSerializer):
model = ProductModel model = ProductModel
fields = "__all__" fields = "__all__"
view_type = { view_type = {
'list': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image'], 'list': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', ],
'slider': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image'], 'slider': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', ],
'instance': ['id', 'name', 'description', 'rating', 'slug', 'meta_description', 'meta_keywords', 'meta_rating', 'category', 'related_products', 'in_pack_items', 'variants', 'colors', 'added_to_favorites', 'image'], 'instance': ['id', 'name', 'description', 'rating', 'slug', 'meta_description', 'meta_keywords', 'meta_rating', 'category', 'related_products', 'in_pack_items', 'variants', 'colors', 'added_to_favorites', 'image'],
'chat': ['id', 'name', 'description', 'variants', 'image'] 'chat': ['id', 'name', 'description', 'variants', 'image']
} }
def get_best_deal_price_before_discount(self, obj):
best_deal = obj.get_best_deal_variant()
if best_deal:
return f'{best_deal.price:,.0f} تومانءءء'
return 0
def get_best_deal_price_after_discount(self, obj):
best_deal = obj.get_best_deal_variant()
if best_deal:
price_after_discount = best_deal.price_after_discount
return f'{price_after_discount:,.0f} تومانءءء'
return 0
def get_best_deal_discount(self, obj):
best_deal = obj.get_best_deal_variant()
if best_deal:
return best_deal.discount
return 0
def get_added_to_favorites(self, obj): def get_added_to_favorites(self, obj):
from account.models import UserFavorites from account.models import UserFavorites