๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/TIL(Today I Learned)

TIL_220621_DRF ๊ฐ•์˜

  • -

DRF ์‹ฌํ™”๊ณผ์ • 2๋ฒˆ์งธ ๊ณผ์ œ..

 

์™€..

 

wa..

 

๋ชจ๋ธ๋ง๋ถ€ํ„ฐ ์‹œ๋ฆฌ์–ผ ์‚ฌ์šฉ๋ฒ•๊ณผ ์‹œ๋ฆฌ์–ผ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ views.py ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•๊นŒ์ง€..

 

ํš๊ธฐ์ ์ด๋ฉด์„œ ๊ฐ„ํŽธํ•˜์ง€๋งŒ

 

์•„์ง๊นŒ์ง€๋„ ์ต์ˆ™ํ•˜์ง€ ๋ชปํ•œ ๊ทธ๊ฒƒ..

 

DRF ๋„ˆ ๋Œ€์ฒด ๋ญ์•ผ... ใ… 

 


 

5์ผ์ฐจ

1. product๋ผ๋Š” ์•ฑ์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”

- ์ƒ์„ฑํ•˜๋ฉด settings.py ๋„ ์ถ”๊ฐ€

python manage.py startapp product



2. product ์•ฑ์—์„œ <์ž‘์„ฑ์ž, ์ œ๋ชฉ, ์ธ๋„ค์ผ, ์„ค๋ช…, ๋“ฑ๋ก์ผ์ž, ๋…ธ์ถœ ์‹œ์ž‘ ์ผ, ๋…ธ์ถœ ์ข…๋ฃŒ์ผ>๊ฐ€ ํฌํ•จ๋œ product ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”

class Product(models.Model):
    user = models.ForeignKey('user.User', verbose_name="์ž‘์„ฑ์ž", on_delete=models.CASCADE)
    title = models.CharField(verbose_name="์ œ๋ชฉ", max_length=50)
    thumbnail = models.FileField("์ธ๋„ค์ผ", upload_to="product/")
    description = models.TextField("์„ค๋ช…")
    created = models.DateTimeField("๋“ฑ๋ก์ผ์ž", auto_now_add=True)
    exposure_start_date = models.DateField("๋…ธ์ถœ ์‹œ์ž‘ ์ผ", default=timezone.now)
    exposure_end_date = models.DateField("๋…ธ์ถœ ์ข…๋ฃŒ ์ผ", default=timezone.now)

    def __str__(self):
        return f"์ œ๋ชฉ : {self.title}"


3. django serializer์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” validate / create / update ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด event ํ…Œ์ด๋ธ”์˜ ์ƒ์„ฑ/์ˆ˜์ • ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”

* postman์œผ๋กœ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ๋•Œ๋Š” raw ๋Œ€์‹  form-data๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , Key type์„ File๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”

- ์‹œ๋ฆฌ์–ผ ์„ธํŒ…์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ผญ ์‹œ๋ฆฌ์–ผ์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์„ค์ •ํ•˜์ž.

class ProductView(APIView):
    permission_classes = [IsAdminOrAuthenticatedReadOnly]
    
    
    # validate 
    def get(self, request):
        today = datetime.now()
        products = ProductModel.objects.filter(
            Q(exposure_start_date__lte = today, exposure_end_date__gte = today,) |
            Q(user=request.user)
        )

        serializerd_data = ProductSerializer(products, many=True).data
        
        return Response(serializerd_data, status=status.HTTP_200_OK)
    
    
    # create 
    #validator validator(์ž…์ฆํ•˜๋‹ค) ์„ค์ •
    def post(self, request):
        '''
        ํ•ญ์ƒ ์œ ์ €์˜ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
        user = request.user  ๋•Œ๋ฌธ์— ์ด ๊ตฌ๋ฌธ์€ ํ•„์š”๊ฐ€ ์—†์Œ
        request.user ์˜ ์ •๋ณด๋ฅผ request.data ์˜ user id ๊ฐ’์— ๋„ฃ์–ด์ฃผ๋Š” ๊ณผ์ •
        '''
        request.data['user'] = request.user.id

        # product ์˜ serializer ๋ฐ์ดํ„ฐ ๊ฐ’์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋„˜๊ฒจ์ค€๋‹ค.
        product_serializer = ProductSerializer(data=request.data)

        # valid() ์ž…์ฆ์ด ์•ˆ๋˜์—ˆ์„ ๊ฒฝ์šฐ return False ๋กœ ๋„˜๊ฒจ์ค€๋‹ค. (๊ฒ€์ฆ๊ณผ์ •)
        if product_serializer.is_valid():
            # validator ๋ฅผ ํ†ต๊ณผํ–ˆ์„ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ์ €์žฅ / .is_valid() ๊ฐ€ ํ†ต๊ณผ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ €์žฅ ๊ฐ€๋Šฅ
            product_serializer.save()

            # return ์œผ๋กœ ์‹œ๋ฆฌ์–ผ ๋ฐ์ดํ„ฐ ๋ณด๋‚ด์ฃผ๊ธฐ
            return Response(product_serializer.data, status=status.HTTP_200_OK)

        '''
        ํ†ต๊ณผ๋ฅผ ๋ชปํ•œ ๊ฒฝ์šฐ
        .error ์—๋Š” validator์— ์‹คํŒจํ•œ ํ•„๋“œ์™€ ์‹คํŒจ ์‚ฌ์œ ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค.
        '''
        return Response(product_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    
    
    # update 
    def put(self, request, product_id):
        product = ProductModel.objects.get(id=product_id)
        '''
        ์œ„์˜ productmodels id ๋ฐ์ดํ„ฐ๋ฅผ ์‹œ๋ฆฌ์–ผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ณ  productserializer ๋กœ ๋‹ค์‹œ request.data ๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
        partial ๋Š” ์ผ๋ถ€ ์ˆ˜์ •์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.
        '''
        product_serializer = ProductSerializer(product, data=request.data, partial=True)

        if product_serializer.is_valid():
            product_serializer.save()

            return Response(product_serializer.data, status=status.HTTP_200_OK)

        return Response(product_serializer.errors, status=status.HTTP_400_BAD_REQUEST)


4. ๋“ฑ๋ก๋œ ์ด๋ฒคํŠธ ์ค‘ ํ˜„์žฌ ์‹œ๊ฐ„์ด ๋…ธ์ถœ ์‹œ์ž‘ ์ผ๊ณผ ๋…ธ์ถœ ์ข…๋ฃŒ ์ผ์˜ ์‚ฌ์ด์— ์žˆ๊ฑฐ๋‚˜, ๋กœ๊ทธ์ธ ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑํ•œ product ์ฟผ๋ฆฌ์…‹์„ ์ง๋ ฌํ™” ํ•ด์„œ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” serializer๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”

class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = ProductModel
        # Product field ์„ค์ •
        fields = [
            'user', 'title', 'thumbnail', 'description',
            'created', 'exposure_start_date', 'exposure_end_date',
            ]

 

5. product field๋ฅผ admin์—์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”

from product.models import Product as ProductModel

# Register your models here.

admin.site.register(ProductModel)

 

'๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ > TIL(Today I Learned)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TIL_220630_rest_framework_simplejwt  (0) 2023.01.01
TIL_220623_๋”ฅ๋Ÿฌ๋‹  (0) 2023.01.01
TIL_220620_DRF ๊ฐ•์˜  (0) 2023.01.01
TIL_220617_DRF ๊ฐ•์˜  (0) 2023.01.01
TIL_220616_DRF ๊ฐ•์˜  (0) 2023.01.01
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.