Django - Passing arguments to models through ForeignKey attributes

Posted by marshall on Stack Overflow See other posts from Stack Overflow or by marshall
Published on 2011-01-14T06:22:44Z Indexed on 2011/01/14 8:53 UTC
Read the original article Hit count: 246

Filed under:
|
|

I've got a class like this:

class Image (models.Model):
    ...
    sizes = ((90,90), (300,250))

    def resize_image(self):
        for size in sizes:
            ...

and another class like this:

class SomeClassWithAnImage (models.Model):
    ...
    an_image = models.ForeignKey(Image)

what i'd like to do with that class is this:

class SomeClassWithAnImage (models.Model):
    ...
    an_image = models.ForeignKey(Image, sizes=((90,90), (150, 120)))

where i'm can specify the sizes that i want the Image class to use to resize itself as a argument rather than being hard coded on the class. I realise I could pass these in when calling resize_image if that was called directly but the idea is that the resize_image method is called automatically when the object is persisted to the db.

if I try to pass arguments through the foreign key declaration like this i get an error straight away. is there an easy / better way to do this before I begin hacking down into django?

© Stack Overflow or respective owner

Related posts about python

Related posts about django