Search code examples
djangotastypie

Django: Tastypie filesize


I may be asking something stupid, but i'm a total noob at django, so...

I have a file upload model, and i'm spitting it out via json. I'd like to be able to also spit out the filzesize of each file. How would i go on about doing that?


Solution

  • You should use dehydrate method and make something like this:

    class MyResource(ModelResource):
        photo = fields.FileField(attribute="photo")
        class Meta:
            queryset = Note.objects.all()
    
        def dehydrate(self, bundle):
            bundle.data['photo_size_in_bytes'] = bundle.obj.photo.size  # size in bytes 
            return bundle
    

    This will add size in bytes to your json.