Skip to content

Commit

Permalink
Product sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
PsychedelicMonkey committed May 20, 2023
1 parent 0515b04 commit 8a49ef6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
11 changes: 8 additions & 3 deletions shop/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib import admin
from .models import Category, Color, Product, ProductImage, Review
from .models import Category, Color, Product, ProductImage, ProductSize, Review, Size

admin.site.register(Category)
admin.site.register(Color)
admin.site.register(Review)
admin.site.register(Size)


class ProductImageInline(admin.TabularInline):
Expand All @@ -13,12 +14,16 @@ class ProductImageInline(admin.TabularInline):
extra = 0


class ProductSizeInline(admin.TabularInline):
model = ProductSize
extra = 0


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
filter_horizontal = ("categories", "colors",)
inlines = (ProductImageInline,)
inlines = (ProductImageInline, ProductSizeInline,)
list_display_links = ("img_tag", "name",)
list_display = ("img_tag", "name", "price",)
list_filter = ("name", "price", "categories",)
prepopulated_fields = {"slug": ["name"]}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.1 on 2023-05-20 20:31

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('shop', '0006_color_alter_category_options_product_colors_and_more'),
]

operations = [
migrations.CreateModel(
name='Size',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('size', models.CharField(max_length=50)),
],
),
migrations.AlterModelOptions(
name='color',
options={'ordering': ('color',)},
),
migrations.CreateModel(
name='ProductSize',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('count', models.PositiveIntegerField(default=1)),
('color', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.color')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.product')),
('size', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shop.size')),
],
),
migrations.AddField(
model_name='product',
name='sizes',
field=models.ManyToManyField(blank=True, through='shop.ProductSize', to='shop.size'),
),
]
18 changes: 18 additions & 0 deletions shop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ def __str__(self):
return self.color


class Size(models.Model):
size = models.CharField(max_length=50, null=False, blank=False)

class Meta:
ordering = ("size",)

def __str__(self):
return self.size


class Product(models.Model):
name = models.CharField(max_length=125, null=False, blank=False)
slug = models.SlugField(max_length=255, null=False, blank=False, unique=True)
Expand All @@ -36,6 +46,7 @@ class Product(models.Model):
quantity = models.IntegerField(null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True)
colors = models.ManyToManyField(Color, blank=True)
sizes = models.ManyToManyField(Size, through="ProductSize", blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

Expand Down Expand Up @@ -110,6 +121,13 @@ def img_tag(self):
return mark_safe(f'<img src="{self.image.url}" alt"{self.caption}" width="150" />')


class ProductSize(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
color = models.ForeignKey(Color, on_delete=models.CASCADE)
size = models.ForeignKey(Size, on_delete=models.CASCADE)
count = models.PositiveIntegerField(default=1)


class Review(models.Model):
class Rating(models.IntegerChoices):
BAD = 1
Expand Down
12 changes: 11 additions & 1 deletion shop/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers
from account.serializers import UserSerializer
from .models import Category, Color, Product, ProductImage, Review
from .models import Category, Color, Product, ProductImage, Review, Size


class CategorySerializer(serializers.ModelSerializer):
Expand All @@ -15,6 +15,15 @@ class Meta:
fields = '__all__'


class SizeSerializer(serializers.ModelSerializer):
count = serializers.IntegerField()
color = ColorSerializer(many=False, read_only=True)

class Meta:
model = Size
fields = '__all__'


class ProductImageSerializer(serializers.ModelSerializer):
color = ColorSerializer(many=False, read_only=True)

Expand All @@ -26,6 +35,7 @@ class Meta:
class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True, read_only=True)
colors = ColorSerializer(many=True, read_only=True)
sizes = SizeSerializer(source="productsize_set", many=True, read_only=False)
images = ProductImageSerializer(many=True, read_only=True)
review_score = serializers.DecimalField(decimal_places=1, max_digits=2)

Expand Down

0 comments on commit 8a49ef6

Please sign in to comment.