Yeah well that was expected to come. I mean DRF would still need to put some values for the serializer objects. May be I don't know, the code that actually loops over and serializer the object and then makes a paginated response could be overridden to not include None objects in the response. But I will have to take a look at the source code. In the mean time, as I already said why don't you try to this from queryset only. Remove the rows that you know are gonna raise an exception at serialization, the way I see it. It will have two advantages, one you won't have to write some custom code to exclude the None objects and second your serialization will become faster.
Why dont you list your model and tell us the requirement you have from the queryset. I have come to find, that django's ORM is damn good. Possibly someone can help you come up with something that you may have missed. On Thu, Jul 2, 2020, 4:47 PM Gagan Deep <the.one.above.all.ti...@gmail.com> wrote: > I have done this to omit the instance completely. > > In models.py > class FooModel(models.Model): > @cached_property > def foo(self): > # Do the operation here > raise CustomException > > In serializers.py > class FooSerializer(serializers.ModelSerializer): > def to_representation(self, instance): > try: > return super().to_representation(instance) > except CustomException: > return None > > Now in the browsable API views I see null for instances which raises > CustomException like this: > { > "count": 5, > "next": null, > "previous": null, > "results": [ > null, > null, > null, > null, > null > ] > } > > > The next thing I want is to completely remove these null values from the > response, Looking at code of ListModelMixin, I don't think it will be > straight forward. > > On Thursday, July 2, 2020 at 3:21:08 PM UTC+5:30, Gagan Deep wrote: >> >> This is all what I am doing, Dipendra. >> >> I have observed that if an inbuilt exception is raised for an object, >> that field get's omitted in the Browsable API list view. I want to achieve >> the same functionality but for whole object. >> >> >> On Thursday, July 2, 2020 at 2:05:55 AM UTC+5:30, DIPENDRA BHATT wrote: >>> >>> Happy to help, >>> Though if you list your exact requirements and what you done to solve >>> them, we might be able to provide much concise and standard solution >>> instead of writing some hacky code. >>> >>> >>> On Thu, Jul 2, 2020, 1:51 AM Gagan Deep <the.one.a...@gmail.com> wrote: >>> >>>> Thanks for such a prompt response. >>>> >>>> I am catching a custom exception here. >>>> >>>> I don't think filtering the queryset will solve the problem since the >>>> exception is raised after performing some operation on properties of >>>> object. I will explore if it can be done through validation. >>>> Thanks again! 😄 >>>> >>>> On Thu, 2 Jul, 2020, 1:43 AM DIPENDRA BHATT, <dipenb...@gmail.com> >>>> wrote: >>>> >>>>> Serializermethodfield is a read only field used to append some custom >>>>> data to the serialization of objects. If you wanna skip this object in >>>>> case >>>>> a exception occurs to fetch the custom field, this probably was a >>>>> validation/filtering issue on the quesryset part which is being supplied >>>>> to >>>>> the serializer class to serialize the queryset. It's better if you do the >>>>> validation/filtering part of the queryset before hand(checkout django >>>>> filters, awesome library) and then when your query set has only those >>>>> objects which you want serialize them. >>>>> >>>>> >>>>> On Thu, Jul 2, 2020, 1:23 AM Brent O'Connor <epic...@gmail.com> wrote: >>>>> >>>>>> What exception is it throwing? >>>>>> >>>>>> On Wednesday, July 1, 2020 at 2:49:46 PM UTC-5, Gagan Deep wrote: >>>>>>> >>>>>>> I have a SerializerMethodField in serializer. I am catching an >>>>>>> exception in the function like below. I want to skip this object in the >>>>>>> ListView entirely if that exception is caught. How can I do this? >>>>>>> >>>>>>> I am using ListApiView from rest_framework.generics >>>>>>> >>>>>>> >>>>>>> classFooSerializer(serializers.ModelSerializer): >>>>>>> foo = serializers.SerializerMethodField() >>>>>>> >>>>>>> get_foo(self, object): >>>>>>> try: >>>>>>> # Do something here >>>>>>> except: >>>>>>> # If an exception is captured than skip this object >>>>>>> >>>>>>> >>>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Django REST framework" group. >>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>> send an email to django-rest-framework+unsubscr...@googlegroups.com. >>>>>> To view this discussion on the web visit >>>>>> https://groups.google.com/d/msgid/django-rest-framework/c15a63b8-b052-4ade-ae0f-c53e57d4a848o%40googlegroups.com >>>>>> <https://groups.google.com/d/msgid/django-rest-framework/c15a63b8-b052-4ade-ae0f-c53e57d4a848o%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>>> . >>>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Django REST framework" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to django-rest-framework+unsubscr...@googlegroups.com. >>>>> To view this discussion on the web visit >>>>> https://groups.google.com/d/msgid/django-rest-framework/CAEiaXZk171ADiBUiuwXPqz-uewDEOug%2BB52U-xvAXdTUhg-uUw%40mail.gmail.com >>>>> <https://groups.google.com/d/msgid/django-rest-framework/CAEiaXZk171ADiBUiuwXPqz-uewDEOug%2BB52U-xvAXdTUhg-uUw%40mail.gmail.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Django REST framework" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to django-rest-framework+unsubscr...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/django-rest-framework/CAOX69xrP3QPTMnEJLW1RAwoDUQcjN7jN%2BmZVPkrc-vtX0WiAmA%40mail.gmail.com >>>> <https://groups.google.com/d/msgid/django-rest-framework/CAOX69xrP3QPTMnEJLW1RAwoDUQcjN7jN%2BmZVPkrc-vtX0WiAmA%40mail.gmail.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- > You received this message because you are subscribed to the Google Groups > "Django REST framework" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-rest-framework+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-rest-framework/392c0ab8-1687-44ae-981b-757599502009o%40googlegroups.com > <https://groups.google.com/d/msgid/django-rest-framework/392c0ab8-1687-44ae-981b-757599502009o%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CAEiaXZkTKaB_gsZygb8WCrWU1qzhi%3D-C2u%3Dq8cmyqkXZnVa7jQ%40mail.gmail.com.