I would like to generate a json which is the aggregation of data from several tables linked together (OneToOne and ManyToMany relationships).
models.py
Team(models.Model):
staff = models.OneToOneField(Staff, on_delete=models.CASCADE, related_name='team_staff')
function = models.CharField()
Staff(models.Model):
firstname = models.CharField()
lastname = models.CharField()
courses = models.ManyToManyField(Course, related_name='staff_course')
Course(models.models):
name = models.CharField()
begin= models.DateField()
end = models.DateField()
def __str__(self):
return '%s - %s' % (self.name, self.begin, self.end)
views.py
def all_team(request):
team_query = Team.objects.all().select_related('staff')
team_list = []
for person in team_query:
courses = Staff.objects.get(id=person.staff.id).courses.order_by('-begin')
team_list.append({'fields': {'id': obj.id,
'function': person.function,
'firstname': person.staff.firstname,
'lastname': person.staff.lastname,
'last_course': str(courses.first()),
}})
json_data = json.dumps(team_list, cls=DjangoJSONEncoder)
return HttpResponse(json_data, content_type='application/json')
As you can see, this is not very efficient. For each member of the team, you make a query to get the last course taken. Can we add something like :
staff_query = Staff.objects.all().prefetch_related(Prefetch('courses', queryset=Course.objects.only('name').all()))
and merge / combine it with the team_query.
Thank you in advance for your advice