add: add new field for product
This commit is contained in:
@@ -7,7 +7,7 @@ class AdminProductListSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'name_uz', 'name_ru', 'image', 'category', 'price', 'description_uz', 'description_ru', 'unity', 'tg_id', 'code', 'article',
|
'id', 'name_uz', 'name_ru', 'image', 'category', 'price', 'description_uz', 'description_ru', 'unity', 'tg_id', 'code', 'article', 'quantity_left'
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_category(self, obj):
|
def get_category(self, obj):
|
||||||
@@ -21,7 +21,7 @@ class ProductSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = [
|
fields = [
|
||||||
'name_uz', 'name_ru', 'image', 'category', 'price', 'description_uz', 'description_ru', 'unity', 'tg_id', 'code', 'article',
|
'name_uz', 'name_ru', 'image', 'category', 'price', 'description_uz', 'description_ru', 'unity', 'tg_id', 'code', 'article', 'quantity_left'
|
||||||
]
|
]
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'image': {'required':False},
|
'image': {'required':False},
|
||||||
@@ -29,6 +29,7 @@ class ProductSerializer(serializers.ModelSerializer):
|
|||||||
'price': {'required':False},
|
'price': {'required':False},
|
||||||
'tg_id': {'required': False},
|
'tg_id': {'required': False},
|
||||||
'code': {'required': False},
|
'code': {'required': False},
|
||||||
'article': {'required': False}
|
'article': {'required': False},
|
||||||
|
'quantity_left': {'required': False},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,12 +48,17 @@ class OrderCreateSerializer(serializers.Serializer):
|
|||||||
total_price = 0
|
total_price = 0
|
||||||
total_price += validated_data.get('delivery_price')
|
total_price += validated_data.get('delivery_price')
|
||||||
for item in order_items:
|
for item in order_items:
|
||||||
|
product = item.get("product")
|
||||||
items.append(OrderItem(
|
items.append(OrderItem(
|
||||||
product=item.get('product'),
|
product=product,
|
||||||
price=item.get('price'),
|
price=item.get('price'),
|
||||||
quantity=item.get('quantity'),
|
quantity=item.get('quantity'),
|
||||||
order=order,
|
order=order,
|
||||||
))
|
))
|
||||||
|
if product.quantity_left > 0:
|
||||||
|
product.quantity_left -= item.get('quantity')
|
||||||
|
product.save()
|
||||||
|
|
||||||
total_price += item.get('price')
|
total_price += item.get('price')
|
||||||
send_orders_to_tg_bot.delay(
|
send_orders_to_tg_bot.delay(
|
||||||
chat_id=item.get('product').tg_id,
|
chat_id=item.get('product').tg_id,
|
||||||
|
|||||||
18
core/apps/products/migrations/0007_product_quantity_left.py
Normal file
18
core/apps/products/migrations/0007_product_quantity_left.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.2 on 2025-09-05 16:57
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('products', '0006_rename_irticle_product_article'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='product',
|
||||||
|
name='quantity_left',
|
||||||
|
field=models.PositiveBigIntegerField(default=0),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -25,6 +25,7 @@ class Product(BaseModel):
|
|||||||
tg_id = models.CharField(max_length=50, null=True, blank=True)
|
tg_id = models.CharField(max_length=50, null=True, blank=True)
|
||||||
article = models.CharField(max_length=200, null=True, blank=True, unique=True)
|
article = models.CharField(max_length=200, null=True, blank=True, unique=True)
|
||||||
code = models.CharField(max_length=200, unique=True, null=True, blank=True)
|
code = models.CharField(max_length=200, unique=True, null=True, blank=True)
|
||||||
|
quantity_left = models.PositiveBigIntegerField(default=0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|||||||
Reference in New Issue
Block a user