Remove ProductStorage model in market app and add PostCreateSerializer
This commit is contained in:
parent
7700664f81
commit
d12bc6cf99
@ -1,11 +1,10 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Brand, Product, ProductStorage, Post, Image
|
||||
from .models import Brand, Product, Post, Image
|
||||
|
||||
|
||||
admin.site.register(Brand)
|
||||
admin.site.register(Product)
|
||||
admin.site.register(ProductStorage)
|
||||
admin.site.register(Image)
|
||||
|
||||
|
||||
@ -13,7 +12,6 @@ admin.site.register(Image)
|
||||
class PostAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"product",
|
||||
"storage",
|
||||
"price",
|
||||
"text",
|
||||
"author",
|
||||
|
@ -10,21 +10,11 @@ class Brand(models.Model):
|
||||
return self.name
|
||||
|
||||
|
||||
class ProductStorage(models.Model):
|
||||
storage = models.CharField(max_length=20)
|
||||
|
||||
def __str__(self):
|
||||
return self.storage
|
||||
|
||||
|
||||
class Product(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
brand = models.ForeignKey(
|
||||
Brand, on_delete=models.CASCADE, related_name="products"
|
||||
)
|
||||
storage = models.ManyToManyField(
|
||||
"ProductStorage", blank=True, related_name="products"
|
||||
)
|
||||
release_date = models.DateField(blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
@ -43,7 +33,6 @@ class Post(models.Model):
|
||||
product = models.ForeignKey(
|
||||
Product, on_delete=models.CASCADE, related_name="posts"
|
||||
)
|
||||
storage = models.ForeignKey(ProductStorage, on_delete=models.CASCADE)
|
||||
price = models.IntegerField()
|
||||
text = models.TextField()
|
||||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
|
@ -10,11 +10,9 @@ from market.models import (
|
||||
|
||||
|
||||
class ProductSerializer(ModelSerializer):
|
||||
storage = serializers.StringRelatedField(many=True)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ("id", "name", "brand", "storage")
|
||||
fields = ("id", "name", "brand")
|
||||
depth = 1
|
||||
|
||||
|
||||
@ -48,14 +46,12 @@ class PostListSerializer(ModelSerializer):
|
||||
product = ProductListSerializer(read_only=True)
|
||||
nickname = serializers.CharField(source="author.nickname")
|
||||
image = serializers.SerializerMethodField()
|
||||
storage = serializers.CharField(source="storage.storage")
|
||||
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = (
|
||||
"id",
|
||||
"product",
|
||||
"storage",
|
||||
"price",
|
||||
"text",
|
||||
"nickname",
|
||||
@ -71,18 +67,26 @@ class PostListSerializer(ModelSerializer):
|
||||
).data["image"]
|
||||
|
||||
|
||||
class PostCreateSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = (
|
||||
"product",
|
||||
"price",
|
||||
"text",
|
||||
)
|
||||
|
||||
|
||||
class PostSerializer(ModelSerializer):
|
||||
product = ProductListSerializer(read_only=True)
|
||||
nickname = serializers.CharField(source="author.nickname")
|
||||
images = ImageSerializer(many=True, read_only=True)
|
||||
storage = serializers.CharField(source="storage.storage")
|
||||
|
||||
class Meta:
|
||||
model = Post
|
||||
fields = (
|
||||
"id",
|
||||
"product",
|
||||
"storage",
|
||||
"price",
|
||||
"text",
|
||||
"nickname",
|
||||
|
@ -10,6 +10,7 @@ from market.serializers import (
|
||||
BrandSerializer,
|
||||
ProductSerializer,
|
||||
PostSerializer,
|
||||
PostCreateSerializer,
|
||||
PostListSerializer,
|
||||
)
|
||||
|
||||
@ -22,11 +23,7 @@ class BrandViewset(ActionBasedMixin, ModelViewSet):
|
||||
|
||||
|
||||
class ProductViewset(ActionBasedMixin, ModelViewSet):
|
||||
queryset = (
|
||||
Product.objects.all()
|
||||
.select_related("brand")
|
||||
.prefetch_related("storage")
|
||||
)
|
||||
queryset = Product.objects.all().select_related("brand")
|
||||
serializer_class = ProductSerializer
|
||||
serializer_class_map = {
|
||||
"posts": PostSerializer,
|
||||
@ -50,12 +47,13 @@ class ProductViewset(ActionBasedMixin, ModelViewSet):
|
||||
class PostViewset(ActionBasedMixin, ModelViewSet):
|
||||
queryset = (
|
||||
Post.objects.all()
|
||||
.select_related("storage", "product", "author", "product__brand")
|
||||
.select_related("product", "author", "product__brand")
|
||||
.prefetch_related("images")
|
||||
)
|
||||
serializer_class = PostSerializer
|
||||
serializer_class_map = {
|
||||
"list": PostListSerializer,
|
||||
"create": PostCreateSerializer,
|
||||
}
|
||||
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
|
||||
permission_classes_map = {
|
||||
|
Loading…
Reference in New Issue
Block a user