gintas / django-picklefield

A pickled object field for Django
MIT License
180 stars 47 forks source link

Accessing raw serialized data #13

Closed chrisspen closed 9 years ago

chrisspen commented 10 years ago

How would you access the raw serialized data stored in a PickledObjectField? I'm using it to store some relatively large chunks of data (around 100MB) and I'd like to calculate the size of this data and cache it in an additional field. But I'm not sure how to access the serialized data directly. I'm currently reserializing it and reading the size, which is quite wasteful since PickledObjectField has already done this internally.

charettes commented 9 years ago

The data is only serialized when it's prepared to be saved in the database and unserialized when it's retrieved; the raw data is never kept in-memory to be accessed.

I'm afraid you'll have to stick to your actual solution or annotate your queryset with a django.db.models.Func expression to retrieve the stored column size:

from django.db import models

MyModel.objects.annotate(
    pickled_size=models.Func(
        models.F('pickled_field'),
        function='bit_length',
        output_field=models.IntegerField()
    )
)