Skip to content

Commit

Permalink
serializer added
Browse files Browse the repository at this point in the history
  • Loading branch information
fivan999 committed Feb 1, 2024
1 parent 48df9b4 commit 3b01f70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions backend/cfehome/api/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import products.models
import products.serializers
import rest_framework.decorators
import rest_framework.response

import django.forms
import django.http


@rest_framework.decorators.api_view(http_method_names=['GET'])
def home(request: django.http.HttpRequest) -> django.http.JsonResponse:
instance = products.models.Product.objects.get(pk=1)
data = django.forms.model_to_dict(instance)
return django.http.JsonResponse(data)
"""список всех товаров"""
instances = products.models.Product.objects.all()
result_data = products.serializers.ProductSerializer(
instances, many=True
).data
return rest_framework.response.Response(result_data)
14 changes: 14 additions & 0 deletions backend/cfehome/products/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import products.models
import rest_framework.serializers


class ProductSerializer(rest_framework.serializers.ModelSerializer):
"""сериализация модели Product"""

class Meta:
model = products.models.Product
fields = [
'title',
'description',
'price',
]

0 comments on commit 3b01f70

Please sign in to comment.