Django_C.R.U.D_주인과 강아지 (views.py_역참조)
🤔 처음 내가 쓴 코드 (views.py)
import json
from django.http import JsonResponse
from django.views import View
from owner.models import Owner, Dog
class OwnerView(View):
def post(self, request):
data = json.loads(request.body)
Owner.objects.create(
name = data['name'],
age = data['age'],
email = data['email']
)
return JsonResponse({'messasge':'created'}, status=201)
def get(self, request):
owner = Owner.objects.all()
results = []
for owner in owners:
results.append(
{
"owner" : owner.dog.owner.name, age,
"dog" : owner.dog.name, age, email,
}
)
return JsonResponse({'resutls':results}, status=200)
class DogView(View):
def post(self, request):
data = json.loads(request.body)
owner = Owner.objects.get(id=data['owner'])
dog = Dog.objects.create(
dog_name = data['name'],
dog_age = data['age'],
owner = owner
)
return JsonResponse({'messasge':'created'}, status=201)
🤗 정다압? 코드
import json
from django.http import JsonResponse
from django.views import View
from owner.models import Owner, Dog
class OwnerView(View):
def post(self, request):
data = json.loads(request.body)
Owner.objects.create(
name = data['name'],
age = data['age'],
email = data['email']
)
return JsonResponse({'messasge':'created'}, status=201)
# def get(self, request):
# owners = Owner.objects.all()
# result = []
#
# for owner in owners:
# owner_information = {
# 'name' : owner.name,
# 'email' : owner.email,
# 'age' : owner.age
# }
# result.append(owner_information)
# return JsonResponse({'result':result}, status=200)
# def get(self, request):
# owners = Owner.objects.all()
# result = []
#
# for owner in owners:
# dogs = owner.dog_set.all()
# dog_list = []
#
# for dog in dogs:
# dog_information = {
# 'name' : dog.name,
# 'age' : dog.age
# }
# dog_list.append(dog_information)
#
# owner_information = {
# 'name' : owner.name,
# 'email' : owner.email,
# 'age' : owner.age,
# 'dogs' : dog_list
# }
# result.append(owner_information)
# return JsonResponse({'result':result}, status=200)
def delete(self, request):
data = json.loads(request.body)
Owner.objects.delete(
name = data['name'],
age = data['age'],
email = data['email']
)
return JsonResponse({'messasge':'deleted'}, status=201)
class DogView(View):
def post(self, request):
try:
data = json.loads(request.body)
owner = Owner.objects.get(id=data['owner_id'])
dog = Dog.objects.create(
name = data['name'],
age = data['age'],
owner = owner
)
return JsonResponse({'messasge':'created'}, status=201)
except Owner.DoesNotExist:
return JsonResponse({'messasge':'Bad Request'}, status=400)
def get(self, request):
dogs = Dog.objects.all()
result = []
for dog in dogs:
dog_information = {
'name' : dog.name,
'age' : dog.age,
'owner' : dog.owner.name
}
result.append(dog_information)
return JsonResponse({'result':result}, status=200)
🥳 오늘 배운 점!
주인(owner) PK에서 강아지(Dog) FK를 참조하려면 역참조를 해야한다!
Author And Source
이 문제에 관하여(Django_C.R.U.D_주인과 강아지 (views.py_역참조)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@cheesechoux/DjangoC.R.U.D주인과-개저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)