Skip an object in ListView

17 views
Skip to first unread message

Gagan Deep

unread,
Jul 1, 2020, 3:49:46 PM7/1/20
to Django REST framework
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


Brent O'Connor

unread,
Jul 1, 2020, 3:53:32 PM7/1/20
to Django REST framework
What exception is it throwing?

DIPENDRA BHATT

unread,
Jul 1, 2020, 4:13:49 PM7/1/20
to django-res...@googlegroups.com
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. 


--
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-fram...@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.

Gagan Deep

unread,
Jul 1, 2020, 4:21:47 PM7/1/20
to django-res...@googlegroups.com
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! 😄

DIPENDRA BHATT

unread,
Jul 1, 2020, 4:35:55 PM7/1/20
to django-res...@googlegroups.com
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. 


Gagan Deep

unread,
Jul 2, 2020, 5:51:08 AM7/2/20
to Django REST framework
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+unsub...@googlegroups.com.

--
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+unsub...@googlegroups.com.

--
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+unsub...@googlegroups.com.

Gagan Deep

unread,
Jul 2, 2020, 7:17:38 AM7/2/20
to Django REST framework
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.

DIPENDRA BHATT

unread,
Jul 2, 2020, 8:05:07 AM7/2/20
to django-res...@googlegroups.com
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.


To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.

--
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-fram...@googlegroups.com.

--
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-fram...@googlegroups.com.

--
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-fram...@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.
Reply all
Reply to author
Forward
0 new messages