Return 在 Django 2.1 中查询模型时的 ManyToManyField ID?
Return ManyToManyField IDs when querying model in Django 2.1?
我有两个非常简单的模型:
class Person(models.Model):
full_name = models.CharField(max_length=120)
class Event(models.Model):
event_date = models.DateField()
short_description = models.CharField(max_length=250)
people_involved = models.ManyToManyField(
Person, blank=True, related_name="people_involved"
)
我想return所有事件,包括people_involved
。
在views.py中,我有以下内容:
def alljson(request):
events = Event.objects.values()
return JsonResponse(list(events), safe=False)
这个returns:
[
{
"id": 9,
"event_date": "2019-01-01",
"short_description": "New Year's Party",
},
...
]
如何在此响应中包含 people_involved
ManyToManyField?
一种可能的方式或者我认为最好的方式是使用 django-restframework-serlizers
link。
如果您不想使用它,那么不幸的是您需要手动生成所有响应,无法获取它们。你可以像下面这样
def alljson(request):
events =[]
for event in Event.objects.values():
response = {}
response['id'] = event.id
response['short_description'] = event.short_description
response['event_date'] = event.event_date
response['people_involved'] = list(event.people_involved.all())
events.append(response)
return JsonResponse(events, safe=False)
您可以这样使用 model_to_dict:
from django.forms.models import model_to_dict
e_list = []
p_list = []
events = Event.objects.all()
for e in events:
e_list.append(model_to_dict(e, fields=['event_date', 'short_description']))
for p in e.people_involved.all():
p_list.append(model_to_dict(p, fields=['full_name']))
data = {'events': e_list, 'people_involved': p_list}
return JsonResponse(data)
model_to_dict
如果您愿意,您可以包含或排除特定字段:)
或
您可以这样使用 serializers:
from django.core import serializers
data = {}
events = Event.objects.all()
s_events = serializers.serialize("json", events)
data['events'] = s_events
for event in events:
data['persons'] = serializers.serialize("json", event.people_involved.all())
return JsonResponse(data)
我有两个非常简单的模型:
class Person(models.Model):
full_name = models.CharField(max_length=120)
class Event(models.Model):
event_date = models.DateField()
short_description = models.CharField(max_length=250)
people_involved = models.ManyToManyField(
Person, blank=True, related_name="people_involved"
)
我想return所有事件,包括people_involved
。
在views.py中,我有以下内容:
def alljson(request):
events = Event.objects.values()
return JsonResponse(list(events), safe=False)
这个returns:
[
{
"id": 9,
"event_date": "2019-01-01",
"short_description": "New Year's Party",
},
...
]
如何在此响应中包含 people_involved
ManyToManyField?
一种可能的方式或者我认为最好的方式是使用 django-restframework-serlizers
link。
如果您不想使用它,那么不幸的是您需要手动生成所有响应,无法获取它们。你可以像下面这样
def alljson(request):
events =[]
for event in Event.objects.values():
response = {}
response['id'] = event.id
response['short_description'] = event.short_description
response['event_date'] = event.event_date
response['people_involved'] = list(event.people_involved.all())
events.append(response)
return JsonResponse(events, safe=False)
您可以这样使用 model_to_dict:
from django.forms.models import model_to_dict
e_list = []
p_list = []
events = Event.objects.all()
for e in events:
e_list.append(model_to_dict(e, fields=['event_date', 'short_description']))
for p in e.people_involved.all():
p_list.append(model_to_dict(p, fields=['full_name']))
data = {'events': e_list, 'people_involved': p_list}
return JsonResponse(data)
model_to_dict
如果您愿意,您可以包含或排除特定字段:)
或
您可以这样使用 serializers:
from django.core import serializers
data = {}
events = Event.objects.all()
s_events = serializers.serialize("json", events)
data['events'] = s_events
for event in events:
data['persons'] = serializers.serialize("json", event.people_involved.all())
return JsonResponse(data)