On Feb 8, 2016 10:00 AM, "learn django" <siddhesh.dive...@gmail.com> wrote: > > 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. >
If you want data displayed in a read only format, then display the days without using any forms. > What is the ideal place & way to decode this information and pass it to django UI. > The way is dependent on how you've stored the data. The place is likely as close to the end of the request/rendering cycle as possible, maybe even in the template rendering, using something akin the obj.get_foo_display() which would be a method call on the model to convert the data from your stored format to something human readable. You may also be doing the conversion in the view of you need to do something with the data that a template operation can't easily perform. > 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. > There's no code there that would ever modify your data, unless the parent class contains such code. If so, the library providing the parent class likely has a read-only variant that you can use. The only time the DB would be affected is if you executed a query set that has a modification action attached to it, such as update() or delete(). Querying the data via filter() or all() should never change it. > 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 class is not configured correctly, which is why you are getting the traceback. Your queryset parameter should not include the list() wrapper. The for loop needs to be contained in another class method that is called at runtime, where you can gather the queryset and execute list() on it at that point so you continue to get fresh data. I'm assuming you are using DRF to provide the view set parent class. Please read up on their documentation to determine the method hook to override and insert your for loop. Even if this code loaded correctly, you would only ever see the Foo objects that were available at the time the process was started, since the class will only be instantiated once, and your queryset parameter will always contain the same list of Foo objects. I doubt that's what you want. Another topic. You mentioned both decryption and decoding. There is a very significant difference between those two concepts. Base64 operations only handle encoding/decoding, not encryption. Granted, to a human, it looks obfuscated and 'encrypted', but to a computer, it is trivial to process and interpret. If you believe that having your data Base64 encoded in the database is providing any sort of security benefit, you are very mistaken. All you've done is made things much more difficult for you as the programmer to convert between B64 and probably Unicode to make things 'human-readable'. It also makes searching and indexing unusable in most cases. Google 'Base64 encryption' if you want more of an explanation. -James -- 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/CA%2Be%2BciUbEaztP%3DJCwo%3D1YxqqAGETyY-Td_wNbb1dJScg1n_geg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.