Add Django backend, 1C integration, deploy config and API docs
Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
This commit is contained in:
1 parent
5f8d37357d
commit
756f40353b
130 files changed
+10066
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,118 @@
|
||||
from django.contrib import admin
|
||||
from django.db.models import Count
|
||||
|
||||
from .models import Category, Product, ProductImage
|
||||
|
||||
|
||||
class ProductImageInline(admin.TabularInline):
|
||||
model = ProductImage
|
||||
extra = 0
|
||||
|
||||
|
||||
@admin.register(Category)
|
||||
class CategoryAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"name",
|
||||
"slug",
|
||||
"parent",
|
||||
"is_active",
|
||||
"sort_order",
|
||||
"products_count",
|
||||
)
|
||||
list_filter = ("is_active",)
|
||||
search_fields = ("name", "external_id")
|
||||
prepopulated_fields = {"slug": ("name",)}
|
||||
autocomplete_fields = ("parent",)
|
||||
actions = ("activate", "deactivate")
|
||||
|
||||
def get_queryset(self, request):
|
||||
return super().get_queryset(request).annotate(products_count=Count("products"))
|
||||
|
||||
@admin.display(description="Товаров", ordering="products_count")
|
||||
def products_count(self, obj):
|
||||
return obj.products_count
|
||||
|
||||
@admin.action(description="Активировать выбранные")
|
||||
def activate(self, request, queryset):
|
||||
updated = queryset.update(is_active=True)
|
||||
self.message_user(request, f"Активировано: {updated}")
|
||||
|
||||
@admin.action(description="Деактивировать выбранные")
|
||||
def deactivate(self, request, queryset):
|
||||
updated = queryset.update(is_active=False)
|
||||
self.message_user(request, f"Деактивировано: {updated}")
|
||||
|
||||
|
||||
@admin.register(Product)
|
||||
class ProductAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"sku",
|
||||
"name",
|
||||
"category",
|
||||
"price",
|
||||
"stock",
|
||||
"stock_status",
|
||||
"is_hit",
|
||||
"is_new",
|
||||
"is_active",
|
||||
)
|
||||
list_filter = ("is_active", "is_hit", "is_new", "stock_status", "category")
|
||||
search_fields = ("name", "sku", "external_id", "brand")
|
||||
readonly_fields = ("external_id", "external_updated_at", "created_at", "updated_at")
|
||||
autocomplete_fields = ("category",)
|
||||
date_hierarchy = "created_at"
|
||||
inlines = [ProductImageInline]
|
||||
actions = (
|
||||
"mark_hit",
|
||||
"unmark_hit",
|
||||
"mark_new",
|
||||
"unmark_new",
|
||||
"activate",
|
||||
"deactivate",
|
||||
)
|
||||
fieldsets = (
|
||||
(
|
||||
"Идентификация",
|
||||
{"fields": ("external_id", "sku", "name", "slug", "brand", "category")},
|
||||
),
|
||||
("Цены и остатки", {"fields": ("price", "old_price", "stock", "stock_status")}),
|
||||
("Описание", {"fields": ("short_description", "description", "attributes")}),
|
||||
("Флаги", {"fields": ("is_hit", "is_new", "is_active")}),
|
||||
("Служебное", {"fields": ("external_updated_at", "created_at", "updated_at")}),
|
||||
)
|
||||
|
||||
@admin.action(description="Отметить как «Хит продаж»")
|
||||
def mark_hit(self, request, queryset):
|
||||
updated = queryset.update(is_hit=True)
|
||||
self.message_user(request, f"Отмечено: {updated}")
|
||||
|
||||
@admin.action(description="Снять «Хит продаж»")
|
||||
def unmark_hit(self, request, queryset):
|
||||
updated = queryset.update(is_hit=False)
|
||||
self.message_user(request, f"Снято: {updated}")
|
||||
|
||||
@admin.action(description="Отметить как «Новинка»")
|
||||
def mark_new(self, request, queryset):
|
||||
updated = queryset.update(is_new=True)
|
||||
self.message_user(request, f"Отмечено: {updated}")
|
||||
|
||||
@admin.action(description="Снять «Новинка»")
|
||||
def unmark_new(self, request, queryset):
|
||||
updated = queryset.update(is_new=False)
|
||||
self.message_user(request, f"Снято: {updated}")
|
||||
|
||||
@admin.action(description="Активировать выбранные")
|
||||
def activate(self, request, queryset):
|
||||
updated = queryset.update(is_active=True)
|
||||
self.message_user(request, f"Активировано: {updated}")
|
||||
|
||||
@admin.action(description="Деактивировать выбранные")
|
||||
def deactivate(self, request, queryset):
|
||||
updated = queryset.update(is_active=False)
|
||||
self.message_user(request, f"Деактивировано: {updated}")
|
||||
|
||||
|
||||
@admin.register(ProductImage)
|
||||
class ProductImageAdmin(admin.ModelAdmin):
|
||||
list_display = ("product", "url", "is_main", "sort_order")
|
||||
search_fields = ("product__name", "product__sku")
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CatalogConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.catalog"
|
||||
verbose_name = "Каталог"
|
||||
@@ -0,0 +1,34 @@
|
||||
import django_filters
|
||||
|
||||
from .models import Product
|
||||
|
||||
|
||||
class ProductFilter(django_filters.FilterSet):
|
||||
category = django_filters.CharFilter(
|
||||
field_name="category__slug", lookup_expr="exact"
|
||||
)
|
||||
category_id = django_filters.NumberFilter(field_name="category_id")
|
||||
price_min = django_filters.NumberFilter(field_name="price", lookup_expr="gte")
|
||||
price_max = django_filters.NumberFilter(field_name="price", lookup_expr="lte")
|
||||
is_hit = django_filters.BooleanFilter(field_name="is_hit")
|
||||
is_new = django_filters.BooleanFilter(field_name="is_new")
|
||||
brand = django_filters.CharFilter(field_name="brand", lookup_expr="iexact")
|
||||
in_stock = django_filters.BooleanFilter(method="filter_in_stock")
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
"category",
|
||||
"category_id",
|
||||
"price_min",
|
||||
"price_max",
|
||||
"is_hit",
|
||||
"is_new",
|
||||
"brand",
|
||||
"in_stock",
|
||||
]
|
||||
|
||||
def filter_in_stock(self, queryset, name, value):
|
||||
if value:
|
||||
return queryset.filter(stock__gt=0)
|
||||
return queryset.filter(stock__lte=0)
|
||||
@@ -0,0 +1,103 @@
|
||||
# Generated by Django 4.2.16 on 2026-11-06 13:26
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Category',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('external_id', models.CharField(max_length=64, unique=True)),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('slug', models.SlugField(max_length=255, unique=True)),
|
||||
('description', models.TextField(blank=True)),
|
||||
('image_url', models.URLField(blank=True, max_length=1000)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='children', to='catalog.category')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'категория',
|
||||
'verbose_name_plural': 'категории',
|
||||
'db_table': 'catalog_category',
|
||||
'ordering': ['sort_order', 'name'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Product',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('external_id', models.CharField(max_length=64, unique=True)),
|
||||
('sku', models.CharField(db_index=True, max_length=64, verbose_name='артикул')),
|
||||
('name', models.CharField(max_length=500)),
|
||||
('slug', models.SlugField(max_length=500, unique=True)),
|
||||
('brand', models.CharField(blank=True, max_length=255)),
|
||||
('short_description', models.TextField(blank=True)),
|
||||
('description', models.TextField(blank=True)),
|
||||
('price', models.DecimalField(decimal_places=2, max_digits=12)),
|
||||
('old_price', models.DecimalField(blank=True, decimal_places=2, max_digits=12, null=True)),
|
||||
('stock', models.IntegerField(default=0)),
|
||||
('stock_status', models.CharField(choices=[('in_stock', 'В наличии'), ('preorder', 'Под заказ'), ('out_of_stock', 'Нет в наличии')], default='out_of_stock', max_length=20)),
|
||||
('is_hit', models.BooleanField(default=False, verbose_name='хит продаж')),
|
||||
('is_new', models.BooleanField(default=False, verbose_name='новинка')),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('attributes', models.JSONField(blank=True, default=dict)),
|
||||
('external_updated_at', models.DateTimeField(blank=True, null=True)),
|
||||
('category', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='products', to='catalog.category')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'товар',
|
||||
'verbose_name_plural': 'товары',
|
||||
'db_table': 'catalog_product',
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='ProductImage',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('url', models.URLField(max_length=1000)),
|
||||
('alt', models.CharField(blank=True, max_length=255)),
|
||||
('sort_order', models.IntegerField(default=0)),
|
||||
('is_main', models.BooleanField(default=False)),
|
||||
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='catalog.product')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'изображение товара',
|
||||
'verbose_name_plural': 'изображения товаров',
|
||||
'db_table': 'catalog_product_image',
|
||||
'ordering': ['sort_order', 'id'],
|
||||
},
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='product',
|
||||
index=models.Index(fields=['category', 'is_active'], name='catalog_pro_categor_891fe8_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='product',
|
||||
index=models.Index(fields=['is_hit'], name='catalog_pro_is_hit_d36eb0_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='product',
|
||||
index=models.Index(fields=['is_new'], name='catalog_pro_is_new_087fb1_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='product',
|
||||
index=models.Index(fields=['price'], name='catalog_pro_price_2d2a4c_idx'),
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,132 @@
|
||||
from django.db import models
|
||||
|
||||
from apps.common.models import TimeStampedModel
|
||||
from apps.common.utils import make_slug
|
||||
|
||||
|
||||
class Category(TimeStampedModel):
|
||||
"""Catalog category. Source of truth is 1C."""
|
||||
|
||||
external_id = models.CharField(max_length=64, unique=True)
|
||||
parent = models.ForeignKey(
|
||||
"self",
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="children",
|
||||
)
|
||||
name = models.CharField(max_length=255)
|
||||
slug = models.SlugField(max_length=255, unique=True)
|
||||
description = models.TextField(blank=True)
|
||||
image_url = models.URLField(max_length=1000, blank=True)
|
||||
is_active = models.BooleanField(default=True)
|
||||
sort_order = models.IntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
db_table = "catalog_category"
|
||||
verbose_name = "категория"
|
||||
verbose_name_plural = "категории"
|
||||
ordering = ["sort_order", "name"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = make_slug(self.name)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class Product(TimeStampedModel):
|
||||
"""Product card. Price, stock and descriptions come from 1C."""
|
||||
|
||||
class StockStatus(models.TextChoices):
|
||||
IN_STOCK = "in_stock", "В наличии"
|
||||
PREORDER = "preorder", "Под заказ"
|
||||
OUT_OF_STOCK = "out_of_stock", "Нет в наличии"
|
||||
|
||||
external_id = models.CharField(max_length=64, unique=True)
|
||||
sku = models.CharField("артикул", max_length=64, db_index=True)
|
||||
name = models.CharField(max_length=500)
|
||||
slug = models.SlugField(max_length=500, unique=True)
|
||||
category = models.ForeignKey(
|
||||
Category,
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="products",
|
||||
)
|
||||
brand = models.CharField(max_length=255, blank=True)
|
||||
short_description = models.TextField(blank=True)
|
||||
description = models.TextField(blank=True)
|
||||
price = models.DecimalField(max_digits=12, decimal_places=2)
|
||||
old_price = models.DecimalField(
|
||||
max_digits=12, decimal_places=2, null=True, blank=True
|
||||
)
|
||||
stock = models.IntegerField(default=0)
|
||||
stock_status = models.CharField(
|
||||
max_length=20,
|
||||
choices=StockStatus.choices,
|
||||
default=StockStatus.OUT_OF_STOCK,
|
||||
)
|
||||
is_hit = models.BooleanField("хит продаж", default=False)
|
||||
is_new = models.BooleanField("новинка", default=False)
|
||||
is_active = models.BooleanField(default=True)
|
||||
attributes = models.JSONField(default=dict, blank=True)
|
||||
external_updated_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
db_table = "catalog_product"
|
||||
verbose_name = "товар"
|
||||
verbose_name_plural = "товары"
|
||||
ordering = ["-created_at"]
|
||||
indexes = [
|
||||
models.Index(fields=["category", "is_active"]),
|
||||
models.Index(fields=["is_hit"]),
|
||||
models.Index(fields=["is_new"]),
|
||||
models.Index(fields=["price"]),
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.sku} — {self.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = make_slug(self.name)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def main_image(self) -> str | None:
|
||||
images = list(self.images.all())
|
||||
for image in images:
|
||||
if image.is_main:
|
||||
return image.url
|
||||
return images[0].url if images else None
|
||||
|
||||
def recalculate_stock_status(self) -> str:
|
||||
if self.stock > 0:
|
||||
self.stock_status = self.StockStatus.IN_STOCK
|
||||
elif self.stock_status == self.StockStatus.PREORDER:
|
||||
self.stock_status = self.StockStatus.PREORDER
|
||||
else:
|
||||
self.stock_status = self.StockStatus.OUT_OF_STOCK
|
||||
return self.stock_status
|
||||
|
||||
|
||||
class ProductImage(TimeStampedModel):
|
||||
product = models.ForeignKey(
|
||||
Product, on_delete=models.CASCADE, related_name="images"
|
||||
)
|
||||
url = models.URLField(max_length=1000)
|
||||
alt = models.CharField(max_length=255, blank=True)
|
||||
sort_order = models.IntegerField(default=0)
|
||||
is_main = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
db_table = "catalog_product_image"
|
||||
verbose_name = "изображение товара"
|
||||
verbose_name_plural = "изображения товаров"
|
||||
ordering = ["sort_order", "id"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.url
|
||||
@@ -0,0 +1,50 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import Category, Product, ProductImage
|
||||
|
||||
|
||||
class CategorySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Category
|
||||
fields = ["id", "name", "slug", "parent", "image_url", "sort_order"]
|
||||
|
||||
|
||||
class ProductImageSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ProductImage
|
||||
fields = ["id", "url", "alt", "sort_order", "is_main"]
|
||||
|
||||
|
||||
class ProductListSerializer(serializers.ModelSerializer):
|
||||
main_image = serializers.CharField(read_only=True)
|
||||
category = CategorySerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
"id",
|
||||
"slug",
|
||||
"name",
|
||||
"sku",
|
||||
"brand",
|
||||
"price",
|
||||
"old_price",
|
||||
"stock_status",
|
||||
"is_hit",
|
||||
"is_new",
|
||||
"main_image",
|
||||
"category",
|
||||
]
|
||||
|
||||
|
||||
class ProductDetailSerializer(ProductListSerializer):
|
||||
images = ProductImageSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta(ProductListSerializer.Meta):
|
||||
fields = ProductListSerializer.Meta.fields + [
|
||||
"short_description",
|
||||
"description",
|
||||
"stock",
|
||||
"attributes",
|
||||
"images",
|
||||
]
|
||||
@@ -0,0 +1,49 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django.test import TestCase
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from .models import Category, Product
|
||||
|
||||
|
||||
class CatalogApiTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.category = Category.objects.create(
|
||||
external_id="cat-1", name="Диваны", slug="divany"
|
||||
)
|
||||
self.product = Product.objects.create(
|
||||
external_id="prod-1",
|
||||
sku="SKU-1",
|
||||
name="Диван Осло",
|
||||
slug="divan-oslo",
|
||||
category=self.category,
|
||||
price=Decimal("49990.00"),
|
||||
stock=5,
|
||||
stock_status=Product.StockStatus.IN_STOCK,
|
||||
is_hit=True,
|
||||
is_new=True,
|
||||
)
|
||||
|
||||
def test_product_list(self):
|
||||
response = self.client.get("/api/v1/catalog/products/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["count"], 1)
|
||||
self.assertEqual(response.data["results"][0]["sku"], "SKU-1")
|
||||
|
||||
def test_hits_endpoint(self):
|
||||
response = self.client.get("/api/v1/catalog/products/hits/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["count"], 1)
|
||||
|
||||
def test_filter_by_price(self):
|
||||
response = self.client.get(
|
||||
"/api/v1/catalog/products/", {"price_min": "50000"}
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["count"], 0)
|
||||
|
||||
def test_product_detail(self):
|
||||
response = self.client.get("/api/v1/catalog/products/divan-oslo/")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["name"], "Диван Осло")
|
||||
@@ -0,0 +1,12 @@
|
||||
from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from .views import CategoryViewSet, ProductViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register("categories", CategoryViewSet, basename="category")
|
||||
router.register("products", ProductViewSet, basename="product")
|
||||
|
||||
urlpatterns = [
|
||||
path("", include(router.urls)),
|
||||
]
|
||||
@@ -0,0 +1,66 @@
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from .filters import ProductFilter
|
||||
from .models import Category, Product
|
||||
from .serializers import (
|
||||
CategorySerializer,
|
||||
ProductDetailSerializer,
|
||||
ProductListSerializer,
|
||||
)
|
||||
|
||||
|
||||
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Category.objects.filter(is_active=True).select_related("parent")
|
||||
serializer_class = CategorySerializer
|
||||
lookup_field = "slug"
|
||||
pagination_class = None
|
||||
|
||||
|
||||
class ProductViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = (
|
||||
Product.objects.filter(is_active=True)
|
||||
.select_related("category")
|
||||
.prefetch_related("images")
|
||||
)
|
||||
serializer_class = ProductListSerializer
|
||||
lookup_field = "slug"
|
||||
filterset_class = ProductFilter
|
||||
search_fields = ["name", "sku", "brand", "description"]
|
||||
ordering_fields = ["price", "created_at", "name"]
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.action == "retrieve":
|
||||
return ProductDetailSerializer
|
||||
return ProductListSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
if self.action == "hits":
|
||||
queryset = queryset.filter(is_hit=True)
|
||||
elif self.action == "new":
|
||||
queryset = queryset.filter(is_new=True)
|
||||
return queryset
|
||||
|
||||
@action(detail=False, methods=["get"])
|
||||
def hits(self, request):
|
||||
return self.list(request)
|
||||
|
||||
@action(detail=False, methods=["get"])
|
||||
def new(self, request):
|
||||
return self.list(request)
|
||||
|
||||
@action(detail=True, methods=["get"])
|
||||
def related(self, request, slug=None):
|
||||
product = self.get_object()
|
||||
queryset = (
|
||||
Product.objects.filter(is_active=True, category=product.category)
|
||||
.exclude(pk=product.pk)
|
||||
.prefetch_related("images")[:8]
|
||||
)
|
||||
serializer = ProductListSerializer(
|
||||
queryset, many=True, context={"request": request}
|
||||
)
|
||||
return Response(serializer.data)
|
||||
Reference in new issue
Block a user