Hi,

> Le 8 févr. 2016 à 19:00, learn django <siddhesh.dive...@gmail.com 
> <mailto:siddhesh.dive...@gmail.com>> a écrit :
> 
> Hi,
> 
> I store some data like email text, headers and from email address in encoded 
> format in the database.
> I want to have a  page where I can display all this information in readonly 
> format.
> 
> What is the ideal place & way to decode this information and pass it to 
> django UI.
> 
> I was thinking of reading all the information in my views.py.
> But then I don't want to affect my DB. aka information in DB should remain 
> encoded.
> 
> eg.
> 
> class FooViewSet(viewsets.ModelViewSet):
>     queryset = Message.objects.all()
> 
> Message object has headers, body and from fields which are encoded before 
> storing in db.
> If I perform any operation on queryset those changes will be reflected in DB 
> so I cannot do that.
> 
> If I do something like below then I get exception for using list as queryset 
> becomes a list object instead of queryset object.
> 
> class FooViewSet(viewsets.ModelViewSet):
>     queryset = list(Message.objects.all())
>     for obj in queryset:
>         body = base64.b64decode(obj.body)
>         obj.body = body

This wil not work for different reasons.
First one is about Python scope. Doing this will get you a fixed list of 
Message unless you restart the server.
Second is you’re using a generic view that expects QuerySet as you’ve noticed. 
Overriding get_queryset would get somewhat bette result but it still wouldn’t 
work as QuerySet are expected for pagination and filtering.

The best place to perform that are to_representation and to_internal_value for 
that field.
You should have a look at the custom fields 
(http://www.django-rest-framework.org/api-guide/fields/#custom-fields 
<http://www.django-rest-framework.org/api-guide/fields/#custom-fields>) for 
guidance about how to handle that.

Regards,
Xavier,
Linovia.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/74175DA3-19EE-4D91-BC16-16374DFA2D45%40linovia.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to