from django.urls import path
from owner.views import OwnersView, DogsView
app_name = 'owner'
urlpatterns = [
path('owner', OwnersView.as_view()),
path('dog', DogsView.as_view()),
]
.name
Owner.objects.get(id=2)).name
def get(self, request):
owners = Owner.objects.all()
results = []
for owner in owners:
dogs = owner.dog_set.all()
dog_list = []
for dog in dogs:
dog_list.append(
{
'dog_name':dog.name,
'dog_age':dog.age,
}
)
results.append(
{
"name" : owner.name,
"email" : owner.email,
"age" : owner.age,
"dog_info" : dog_list,
}
)
return JsonResponse({'result':results}, status=200)
>>> a = owner.dog_set.all() >>> print(a) <QuerySet [<Dog: Dog object (3)>, <Dog: Dog object (4)>]>
<์ฐธ๊ณ >