On Sep 5, 2017 11:21 PM, "Rakhee Menon" <[email protected]> wrote:
Customization in the sense..I have lot of calculations to do so its not
possible using viewset.
Ah, that isn't reflected in the example code you provided. Fair enough.
Custom logic like calculations can be integrated in to any view or viewset.
Often times the logic is broken out in to separate standalone functions and
called by some simple overrides on the view[set].
You can do everything with an APIView if you'd like, but you may be signing
up for more work than necessary. If you have your heart set on APIView for
whatever reason, you can tweak your code by combining your two get()
definitions in to one:
from django.shortcuts import get_object_or_404
class FormList(APIView):
def get(self, request, id=None):
if id:
obj_data = get_object_or_404(ItemMaster, id=id)
many = False
else:
obj_data = ItemMaster.objects.all()
many = True
serializer = ItemMasterSerializer(obj_data, many=many)
return Response(serializer.data, content_type="application/json")
Admittedly I haven't dealt with DRF serializers in a while, but I believe
that will work. YMMV.
-James
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXu0XTa5r0uLedv4SAMDsp_S%3DhUHg7Vpxp7z3xhDxWqKQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.