add: add new model and api for products
This commit is contained in:
@@ -0,0 +1 @@
|
||||
venv
|
||||
|
||||
@@ -2,10 +2,15 @@ from django.contrib import admin
|
||||
|
||||
from modeltranslation.admin import TranslationAdmin
|
||||
|
||||
from core.apps.products.models import Product
|
||||
from core.apps.products.models import Product, Unity
|
||||
|
||||
|
||||
@admin.register(Product)
|
||||
class ProductAdmin(TranslationAdmin):
|
||||
list_display = ['id', 'name', 'price', 'category']
|
||||
list_filter = ['category']
|
||||
|
||||
|
||||
@admin.register(Unity)
|
||||
class UnityAdmin(admin.ModelAdmin):
|
||||
list_display = ['id', 'name']
|
||||
32
core/apps/products/migrations/0003_unity_product_unity.py
Normal file
32
core/apps/products/migrations/0003_unity_product_unity.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 5.2 on 2025-09-02 14:04
|
||||
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('products', '0002_category_name_ru_category_name_uz_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Unity',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('name', models.CharField(max_length=200)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Birlik',
|
||||
'verbose_name_plural': 'Birliklar',
|
||||
},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='product',
|
||||
name='unity',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='products', to='products.unity'),
|
||||
),
|
||||
]
|
||||
@@ -4,12 +4,24 @@ from core.apps.shared.models.base import BaseModel
|
||||
from core.apps.products.models import Category
|
||||
|
||||
|
||||
class Unity(BaseModel):
|
||||
name = models.CharField(max_length=200)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Birlik'
|
||||
verbose_name_plural = 'Birliklar'
|
||||
|
||||
|
||||
class Product(BaseModel):
|
||||
name = models.CharField(max_length=200)
|
||||
image = models.ImageField(upload_to='products/product/')
|
||||
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')
|
||||
price = models.PositiveBigIntegerField()
|
||||
description = models.TextField(null=True, blank=True)
|
||||
unity = models.ForeignKey(Unity, on_delete=models.CASCADE, related_name='products', null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
@@ -17,4 +29,3 @@ class Product(BaseModel):
|
||||
class Meta:
|
||||
verbose_name = 'Mahsulot'
|
||||
verbose_name_plural = 'Mahsulotlar'
|
||||
|
||||
|
||||
@@ -6,12 +6,19 @@ from core.apps.accounts.models import Like
|
||||
|
||||
class ProductListSerializer(serializers.ModelSerializer):
|
||||
liked = serializers.SerializerMethodField(method_name='get_liked')
|
||||
unity = serializers.SerializerMethodField(method_name='get_unity')
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = [
|
||||
'id', 'name', 'image', 'price', 'description', 'liked'
|
||||
'id', 'name', 'image', 'price', 'description', 'liked', 'unity',
|
||||
]
|
||||
|
||||
def get_liked(self, obj):
|
||||
return Like.objects.filter(user=self.context.get('user'), product=obj).exists()
|
||||
|
||||
def get_unity(self, obj):
|
||||
return {
|
||||
'id': obj.unity.id,
|
||||
'name': obj.unity.name,
|
||||
}
|
||||
@@ -14,6 +14,7 @@ urlpatterns = [
|
||||
[
|
||||
path('<uuid:category_id>/list/', product_views.ProductListApiView.as_view()),
|
||||
path('list/', product_views.ProductsApiView.as_view()),
|
||||
path('<uuid:id>/', product_views.ProductDetailApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
path('search/', search_views.SearchApiView.as_view()),
|
||||
|
||||
@@ -9,12 +9,12 @@ from core.apps.products.serializers import product as serializers
|
||||
|
||||
class ProductListApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.ProductListSerializer
|
||||
queryset = Product.objects.all()
|
||||
queryset = Product.objects.select_related('unity')
|
||||
permission_classes = []
|
||||
|
||||
def get(self, request, category_id):
|
||||
category = get_object_or_404(Category, id=category_id)
|
||||
products = Product.objects.filter(category=category)
|
||||
products = Product.objects.filter(category=category).select_related('unity')
|
||||
page = self.paginate_queryset(products)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True, context={'user': request.user})
|
||||
@@ -25,14 +25,25 @@ class ProductListApiView(generics.GenericAPIView):
|
||||
|
||||
class ProductsApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.ProductListSerializer
|
||||
queryset = Product.objects.all()
|
||||
queryset = Product.objects.select_related('unity')
|
||||
permission_classes = []
|
||||
|
||||
def get(self, request):
|
||||
products = Product.objects.all()
|
||||
products = self.queryset
|
||||
page = self.paginate_queryset(products)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
serializer = self.serializer_class(products, many=True)
|
||||
return Response(serializer.data, status=200)
|
||||
|
||||
|
||||
class ProductDetailApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.ProductListSerializer
|
||||
queryset = Product.objects.select_related('unity')
|
||||
permission_classes = []
|
||||
|
||||
def get(self, request, id):
|
||||
product = get_object_or_404(Product, id=id)
|
||||
serializer = self.serializer_class(product)
|
||||
return Response(serializer.data, status=200)
|
||||
Reference in New Issue
Block a user