I need help to convert the below class-based LISTVIEW to the rest framework LISTAPIVIEW because I want to authenticate users using permission classes simple JWT authentication. thanks
class MessagesModelList(ListView):
http_method_names = ['get', ]
paginate_by = getattr(settings, 'MESSAGES_PAGINATION', 500)
def get_queryset(self):
if self.kwargs.get('dialog_with'):
qs = MessageModel.objects \
.filter(Q(recipient=self.request.user, sender=self.kwargs['dialog_with']) |
Q(sender=self.request.user, recipient=self.kwargs['dialog_with'])) \
.select_related('sender', 'recipient')
else:
qs = MessageModel.objects.filter(Q(recipient=self.request.user) |
Q(sender=self.request.user)).prefetch_related('sender', 'recipient', 'file')
return qs.order_by('-created')
def render_to_response(self, context, **response_kwargs):
user_pk = self.request.user.pk
data = [serialize_message_model(i, user_pk)
for i in context['object_list']]
page: Page = context.pop('page_obj')
paginator: Paginator = context.pop('paginator')
return_data = {
'page': page.number,
'pages': paginator.num_pages,
'data': data
}
return JsonResponse(return_data, **response_kwargs)
you need to create the below files and and try the following
serializer.py
views.py
urls.py
This is a basic example of how you can create ListView using DRF. if you need more customization, please refer the documentation : https://www.django-rest-framework.org/api-guide/generic-views/