🖥 rest framework 표준
# ==== models.py ====
from django.db import models
class Patient(models.Model):
pid = models.CharField(max_length=100, default="")
pname = models.CharField(max_length=100, default="")
created_date = models.DateField(default=timezone.now)
# ==== serializer.py ====
from rest_framework.serializers import ModelSerializer
from .models import Patient
class PatientSerializer(serializers.ModelSerializer):
class Meta:
model = Patient
fields = "__all__"
# ==== views.py ====
from rest_framework.viewsets import ModelViewSet
from .serializers import PatientSerializer
from .models import Post
class PatientViewSet(ModelViewSet):
queryset = Patient.object.all()
serializer_class = PatientSerializer
# ==== urls.py ====
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultFrouter()
router.register('patients', views.PatientsViewSet)
print(router.urls) # url pattern list
urlpatterns = [
path('', include(router.urls))
]
[rest framework를 사용하기 전]
[rest framework를 사용 후]
🖥 기본적인 serializer 사용법
# ==== serializer.py ====
from rest_framework import serializers
from .models import Patient
class PatientSerializer(serializers.ModelSerializer):
class Meta:
model = Patient
fields = "__all__"
# ==== views.py ====
from .serializers import PatientSerializer
from django.http import JsonResponse
from rest_framework.renderers import JSONRenderer
from rest_framework.viewsets import ModelViewSet
"""
실제 활용을 위한 아래의 코드 Description:
1) PatientViewSet -> create Patient
2) PatientListAPIView -> Load Patient List
"""
# 임시) 첫번째 patient 정보를 serializer를 통해 조회
first_patient = Patient.objects.first()
serializer = PatientSerializer(data=first_patient)
# 임시) 모든 patient 정보를 serializer를 통해 조회
all_patient = Patient.objects.all()
serializer = PatientSerializer(data=all_patient, many=True)
class PatientViewSet(ModelViewSet):
queryset = Patient.object.all()
serializer_class = PatientSerializer
class PatientListAPIView(generics.ListCreateAPIView):
queryset = Patient.objects.all()
serializer_class = PatientSerializer
# ==== urls.py ====
urlpatterns = [
path('patientlist/', views.PatientListAPIView.as_view()),
path('', include(router.urls))
]
🖥 Serializer를 사용한 유효성 검증
# ==== views.py ====
if serializer.is_valid():
return JsonResponse(serializer.data, status=201) # success
return JsonResponse(serializer.errors, status=400)
🖥 방법1) 직접 변환 JSON Encoder
# ==== views.py ====
from django.core.serializers.json import DjangoJSONEncoder
data = [
{'pid': patient.pid,
'pname': patient.pname,
'created_date': patient.created_date}
for patient in Patient.objects.all()
]
json.dumps(data, cls=DjangoJSONEncoder, ensure_ascii=False)
🖥 방법2) JSONRenderer 사용
# ==== views.py ====
FROM rest_framework.renderers import JSONRenderer
json_utf8_string = JSONRenderer().render(serializer.data)
🖥 방법3) DjangoJSONEncoder 사용
# ==== views.py ====
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models.query import QuerySet
class MyJsonEncoder(DjangoJSONEncoder):
def default(self, obj):
if isinstance(obj, QuerySet):
return tuple(obj)
elif isinstance(obj, Patient):
return {'pid': obj.pid, 'pname': obj.pname, 'created_date': obj.created_date}
elif hasattr(obj, 'as_dict'):
return obj.as_dict()
return super().default(obj)
data = Patient.object.all()
json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)