I have various database fields that are decrypted using the to_python method. The problem I’m having is that every time I access the admin section (admin.ModelAdmin list) for this particular model, the page takes ages to load. I don’t need the to_python method to decrypt fields on the list page, as the fields or the columns I’m listing aren’t encrypted at all. When a user clicks on an item to edit it, the to_python method kicks in, which is fine, exactly as intended. However, how can I stop the to_python method trying to decrypt the objects while listing them, bearing in mind the list view shows only fields that are not encrypted? Also, this is causing the filter to work really slowly. Again, the filtering is only done on non-encrypted fields so there shouldn’t be a need for the to_python method to execute itself.
I have various database fields that are decrypted using the to_python method. The problem
Share
So the issue is that the admin will load the entire model instance for each row, even though you’re only displaying a few fields. The solution should be to override the
querysetmethod in yourModelAdminsubclass so that instead of using the standardget_query_setmethod of the default manager, it uses theonlymethod to only get the fields you want:(or you could use
defer('my_expensive_field'), which amounts to the same thing).