Question

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?

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top