How can I convert the following Django view to return JSON with Django Rest Framework? -


i returning bunch of tuples following code. returned values involves data number of different models. wondering how serialize data , return in json format.

def stream(request):     questions = question.objects.all()     answers = answer.objects.filter(hearts__gte=1)     combined = sorted(chain(questions, answers), key=attrgetter('created'), reverse=true)     queryset = []     item in combined:         if hasattr(item, 'parent'):             try:                 qs_heart = answerheart.objects.get(answer=item, user=request.user)                 if qs_heart:                     hearted = true             except answerheart.doesnotexist:                 hearted = false             try:                 qs_flag = answerflag.objects.get(answer=item, user=request.user)                 if qs_flag:                     flagged = true             except answerflag.doesnotexist:                 flagged = false             queryset.append((item, hearted, flagged))         else:             try:                 qs_heart = questionheart.objects.get(question=item, user=request.user)                 if qs_heart:                     hearted = true             except questionheart.doesnotexist:                 hearted = false             try:                 qs_flag = questionflag.objects.get(question=item, user=request.user)                 if qs_flag:                     flagged = true             except questionflag.doesnotexist:                 flagged = false             queryset.append((item, hearted, flagged))     comments = questioncomment.objects.all()     return render(request, "alpha/stream.html", {'queryset': queryset, 'comments': comments}) 

can't this?

class mythingserializer(serializers.serializer):     my_thing_email = serializers.emailfield()  def my_view(request):     serializer = mythingserializer(my_things, many=true)      return httpresponse(jsonrenderer().render(serializer.data), content_type='application/json') 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -