Le 8 févr. 2016 à 19:00, learn django <siddhesh.divekar@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) for guidance about how to handle that.
Regards,
Xavier,
Linovia.
No comments:
Post a Comment