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-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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CAOX69xrP3QPTMnEJLW1RAwoDUQcjN7jN%2BmZVPkrc-vtX0WiAmA%40mail.gmail.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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/c15a63b8-b052-4ade-ae0f-c53e57d4a848o%40googlegroups.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.
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.
--
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.
class FooModel(models.Model):
@cached_property
def foo(self):
# Do the operation here
raise CustomException
class FooSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
try:
return super().to_representation(instance)
except CustomException:
return None
{
"count": 5,
"next": null,
"previous": null,
"results": [
null,
null,
null,
null,
null
]
}
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.
--
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/CAEiaXZk171ADiBUiuwXPqz-uewDEOug%2BB52U-xvAXdTUhg-uUw%40mail.gmail.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/CAOX69xrP3QPTMnEJLW1RAwoDUQcjN7jN%2BmZVPkrc-vtX0WiAmA%40mail.gmail.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.