On Sat, 2007-03-03 at 10:28 +0000, omat * gezgin.com wrote:
> I have an ImageWithThumbs field class derived from ImageField class
> that manages thumbnails of an image. It takes list of dimensions as a
> parameter and handles the creation and deletion of the resized
> images.
>
> I want to dynamically add methods to return the url of a resized image
> when its dimensions are given so that I can access them from the
> templates.
>
> In other words, if there is an image resized to 60 x 60 pixels, there
> should be a method called image_60x60_url which will return the url of
> the resized image.
>
> I tried setattr in the save_file method in the field class as follows:
>
> setattr(self, 'image_%dx%d_url' % (width, height), self.get_url(dim))
>
> where get_url is the method that takes the dimension of the image
> being created and returns the url to that image.
>
> This didn't work, because, I think this method does not persist in the
> class' scope.
This is because you are setting the attribute on the instance, not the
class itself. You want to do
setattr(self.__class__, ....)
and that should work, although in your above example it will add a
constant, not a function object. You either want to investigate how
django.utils.functional.curry works or, for more simple functions,
assign an anonymous function (a lambda expression). Remember that you
want to assign a function that takes at least one argument ("self" or
whatever you want to call it).
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---