๐Ÿ›์žฅ๊ณ ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ(DRF,serializer)

ํŒ”๋ฆฌ๋™ยท2021๋…„ 4์›” 20์ผ
0

๐Ÿ›์žฅ๊ณ ๋กœ ์‡ผํ•‘๋ชฐ ๋งŒ๋“ค๊ธฐ(DRF,serializer)

serializer.py

from rest_framework import serializers
from .models import Product

class ProductSerializer(serializers.ModelSerializer):

    class Meta:
        model = Product
        fields = '__all__'

product.views.py

from rest_framework import generics
from rest_framework import mixins

class ProductListAPI(generics.GenericAPIView, mixins.ListModelMixin):
    serializer_class = ProductSerializer

    def get_queryset(self):
        return Product.objects.all().order_by('id')

    def get(self, request , *args , **kwargs):
        return self.list(request, *args, **kwargs)

class ProductDetailAPI(generics.GenericAPIView, mixins.RetrieveModelMixin):
    serializer_class = ProductSerializer

    def get_queryset(self):
        return Product.objects.all().order_by('id')

    def get(self, request , *args , **kwargs):
        return self.retrieve(request, *args, **kwargs)
  • ProductListAPI๋Š” ์ œํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ProductDetailAPI๋Š” ์ œํ’ˆ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
  • json type์œผ๋กœ ๋ณด์—ฌ์ค€๋‹ค.

url ์—ฐ๊ฒฐ

urlpatterns = [
    .
    .
    .
    path('api/product/', ProductListAPI.as_view()),
    path('api/product/<int:pk>/', ProductDetailAPI.as_view()),

]
  • url ์—ฐ๊ฒฐ์„ ํ†ตํ•ด viewํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ํ–ˆ๋‹ค.

url ์ ‘์†

  • list

  • detail

templates

{% extends "base.html" %}
{% load humanize %}
{% block header %}
<script>
  function product_detail(id) {
    $.ajax({
      url: "/api/product/" + id,
      success: function (result) {
        $("#product-" + id).popover({
          html: true,
          content: result.name + "<br/>" + result.price
        }).popover('show');
      }
    });
  }

  function product_leave(id) {
    $("#product-" + id).popover('hide');
  }
  $(document).ready(function () {});
</script>
{% endblock %}
{% block contents %}
<div class="row mt-5">
  <div class="col-12">
    <table class="table table-light">
      <thead class="thead-light">
        <tr>
          <th scope="col">#</th>
          <th scope="col">์ƒํ’ˆ๋ช…</th>
          <th scope="col">๊ฐ€๊ฒฉ</th>
          <th scope="col">๋“ฑ๋ก๋‚ ์งœ</th>
        </tr>
      </thead>
      <tbody class="text-dark">
        {% for product in product_list %}
        <tr>
          <th scope="row">{{ product.id }}</th>
          <th><a id="product-{{ product.id }}" onmouseenter="product_detail({{ product.id }});"
              onmouseleave="product_leave({{ product.id }});" href="/product/{{ product.id }}">{{ product.name }}</a>
          </th>
          <th>{{ product.price|intcomma }} ์›</th>
          <th>{{ product.register_date|date:'Y-m-d H:i' }}</th>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>

{% endblock %}
  • jquery๋ฅผ ํ™œ์šฉํ•ด์„œ ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • jquery์— ์‹ ๊ฒฝ์“ฐ๊ณ  ์‹ถ์ง€ ์•Š์•„์„œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ–ˆ๋‹ค.

๊ฒฐ๊ณผ

  • ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ฆฌ๋ฉด api์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ์—์„œ ์ œํ’ˆ๋ช…๊ณผ ์ œํ’ˆ๊ฐ€๊ฒฉ์„ ํ‘œ์‹œํ•ด์ค€๋‹ค.
profile
๋ฐฐ์›€์˜ ๊ธฐ๋ก

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด