Re: Skip an object in ListView

2020-07-01 Thread DIPENDRA BHATT
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 
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, 
> 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  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
>>> 
>>> .
>>>
>> --
>> 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
>> 
>> .
>>
> --
> 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
> 
> .
>

-- 
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/CAEiaXZ%3DLcdv37gs-cSjdG%2BdGkHQWm_C%2BXDm0_LHevb7azyJfQQ%40mail.gmail.com.


Re: Skip an object in ListView

2020-07-01 Thread Gagan Deep
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,  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  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
>> 
>> .
>>
> --
> 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
> 
> .
>

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


Re: Skip an object in ListView

2020-07-01 Thread DIPENDRA BHATT
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  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
> 
> .
>

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


Re: Skip an object in ListView

2020-07-01 Thread Brent O'Connor
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.


Skip an object in ListView

2020-07-01 Thread Gagan Deep
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/634738be-bc9d-4178-86f2-252b96430996o%40googlegroups.com.


Re: Django Rest Framework Validation Order Issue

2020-07-01 Thread Brent O'Connor
Hi Özgür,

I'm not sure if you saw it or not but I did come up with a solution in 
another branch 
(https://github.com/epicserve/drf-validation-issue/blob/fix-validation/pizzas/serializers.py)
 
and I did end up overriding *run_validation* and *to_internal_value*. If I 
can find time, I think I'll look into putting my solution into a fork of 
DRF and just see what might break when I run tests. It doesn't seem like 
anything would break, but I'm not confident that is the case.

On Wednesday, July 1, 2020 at 6:02:53 AM UTC-5, Özgür Akçalı wrote:
>
> I think you also need to override *run_validators *method in your example 
> project for it to work as you expect, just like you did for 
> *to_internal_value. 
> *If a validation error is raised fom run_validators method, *validate *method 
> is not called, so you can not aggregate any possible errors raised from 
> validation checks in that method.
>
> Also, with this flow, you should take care not to raise validation errors 
> in your custom *validate *methods one by one, but aggregate them and 
> raise them or return them together there as well.
>
> On Wednesday, July 1, 2020 at 1:45:00 PM UTC+3, Özgür Akçalı wrote:
>>
>> Though you can not achieve what you want by just overriding *validate, *and 
>> not overriding *run_validation*. 
>>
>> This would be a major change in DRF though, as with the current behavior, 
>> when you write your *validate *method, you can assume other validations 
>> passed, so I imagine changing this behavior as default now would break a 
>> lot of existing code. I think this would fit better as an optional 
>> addition, maybe with a configuration option on existing serializer classes, 
>> or with a different set of serializer classes.
>>
>> On Wednesday, July 1, 2020 at 1:59:33 AM UTC+3, Brent O'Connor wrote:
>>>
>>> I guess my point is that I don't feel like I should have to override 
>>> anything and this should be DRF's default behavior.  Thank you for 
>>> pointing out that `validation` should be the method that should be used. 
>>>
>>> On Tue, Jun 30, 2020, 5:42 PM Carl Nobile  wrote:
>>>
 Brent, overriding the 'to_internal_value' method is normal and 
 encouraged, however, the 'run_validation' shouldn't generally be 
 overridden, what you can override is the 'validate' method which is called 
 by 'run_validation'.
 ~Carl

 On Tue, Jun 30, 2020 at 5:51 PM Brent O'Connor  
 wrote:

> For anyone that might come across this thread, I went ahead and 
> created an issue 
>  for 
> this on Github.
>
> On Friday, June 26, 2020 at 5:08:03 PM UTC-5, Brent O'Connor wrote:
>>
>> I created an example project 
>>  to illustrate 
>> this issue, but basically if you have custom validation logic on a 
>> serializer and the data has a field that isn't valid, then the errors 
>> raised are only the field level errors and not the custom validation 
>> logic 
>> errors. This ends up being a bad user experience because the user can 
>> fix 
>> the field error and then when post another request they can end up 
>> getting 
>> errors thrown in the custom validation. I'm thinking this might need to 
>> be 
>> an issue added the DRF project on Github, but thought I would check here 
>> first. Please see the project example for more details.
>>
> -- 
> 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/ea8a9b5d-a671-42b4-9cee-043f28f8efa3o%40googlegroups.com
>  
> 
> .
>


 -- 

 ---
 Carl J. Nobile (Software Engineer)
 carl@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+unsubscr...@googlegroups.com.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQL2UvWyjr6UgK%2BpCDexAc%2BC%2BeBUGxAwZYyaxC3dJYwGsQ%40mail.gmail.com
  
 

Re: Django Rest Framework Validation Order Issue

2020-07-01 Thread Özgür Akçalı
I think you also need to override *run_validators *method in your example 
project for it to work as you expect, just like you did for *to_internal_value. 
*If a validation error is raised fom run_validators method, *validate *method 
is not called, so you can not aggregate any possible errors raised from 
validation checks in that method.

Also, with this flow, you should take care not to raise validation errors 
in your custom *validate *methods one by one, but aggregate them and raise 
them or return them together there as well.

On Wednesday, July 1, 2020 at 1:45:00 PM UTC+3, Özgür Akçalı wrote:
>
> Though you can not achieve what you want by just overriding *validate, *and 
> not overriding *run_validation*. 
>
> This would be a major change in DRF though, as with the current behavior, 
> when you write your *validate *method, you can assume other validations 
> passed, so I imagine changing this behavior as default now would break a 
> lot of existing code. I think this would fit better as an optional 
> addition, maybe with a configuration option on existing serializer classes, 
> or with a different set of serializer classes.
>
> On Wednesday, July 1, 2020 at 1:59:33 AM UTC+3, Brent O'Connor wrote:
>>
>> I guess my point is that I don't feel like I should have to override 
>> anything and this should be DRF's default behavior.  Thank you for 
>> pointing out that `validation` should be the method that should be used. 
>>
>> On Tue, Jun 30, 2020, 5:42 PM Carl Nobile  wrote:
>>
>>> Brent, overriding the 'to_internal_value' method is normal and 
>>> encouraged, however, the 'run_validation' shouldn't generally be 
>>> overridden, what you can override is the 'validate' method which is called 
>>> by 'run_validation'.
>>> ~Carl
>>>
>>> On Tue, Jun 30, 2020 at 5:51 PM Brent O'Connor  
>>> wrote:
>>>
 For anyone that might come across this thread, I went ahead and created an 
 issue  
 for this on Github.

 On Friday, June 26, 2020 at 5:08:03 PM UTC-5, Brent O'Connor wrote:
>
> I created an example project 
>  to illustrate 
> this issue, but basically if you have custom validation logic on a 
> serializer and the data has a field that isn't valid, then the errors 
> raised are only the field level errors and not the custom validation 
> logic 
> errors. This ends up being a bad user experience because the user can fix 
> the field error and then when post another request they can end up 
> getting 
> errors thrown in the custom validation. I'm thinking this might need to 
> be 
> an issue added the DRF project on Github, but thought I would check here 
> first. Please see the project example for more details.
>
 -- 
 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/ea8a9b5d-a671-42b4-9cee-043f28f8efa3o%40googlegroups.com
  
 
 .

>>>
>>>
>>> -- 
>>>
>>> ---
>>> Carl J. Nobile (Software Engineer)
>>> carl@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+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQL2UvWyjr6UgK%2BpCDexAc%2BC%2BeBUGxAwZYyaxC3dJYwGsQ%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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/86b2a1e3-21cf-466a-920c-4645b8125af4o%40googlegroups.com.