codingjoe/django-stdimage

get all available variations

vchrisb opened this issue · 2 comments

I'm writing a serialising method for my StdImageField.
To be able to iterate through the variations on the StdImageField object, I need to know the variation names.
I haven't found an existing option to easily retrieve all the variations.

For testing, I added a very simple function to StdImageFieldFile:

def get_variations(self):
    return self.field.variations

and use it in my serializer:

def get_source(self, obj):
    sources = []
    for variation in obj.source.get_variations():
        variation_obj = getattr(obj.source,variation)
        sources.append({'url': variation_obj.url, 'heigth': variation_obj.height, 'width': variation_obj.width })
    return sources

If I didn't miss an existing option, I create a PR for this functionality

Hi @vchrisb,

There is not get_variations method, but a variations attribute. That being said, are you developing a DRF API? I would certainly welcome a stdimage DRF field in a PR.

Best,
Joe

thx. I missed that I can access it via .field.variations
For now, I created the method get_source to be used for source = serializers.SerializerMethodField() in my ModelSerializer.
I use this information to generate the srcset in the javascript client.