Question

I am trying to override some default values in a inherited Django model. I have a bunch of different image sizes for models I need and the fields needed are 90% the same.

I have tried creating a base model to use and was going to add any additional fields needed to the child models.

The problem I am having is that the images are only using the "default" values I have set and are not being overwritten in the child model. Is what I am trying to accomplish possible?

Thanks!

class ImageLink(models.Model):

    #Default Image Sizes
    SIZED_WIDTH =  500
    SIZED_HEIGHT = 400

    THUMB_WIDTH = 50
    THUMB_HEIGHT = 50


    #Name of the link
    name = models.CharField(max_length = 15)

    #Images used for link
    image_original = models.ImageField(upload_to="imageLink/images/%Y/%m/%d")

    image_sized = ImageSpecField(   source='image_original',
                                processors=[ResizeToFill(SIZED_WIDTH, SIZED_HEIGHT)],
                                format='JPEG',
                                options={'quality' : 60 })

    image_thumb = ImageSpecField(   source='image_original',
                                processors=[ResizeToFill(THUMB_WIDTH, THUMB_HEIGHT)],
                                format='JPEG',
                                options={'quality' : 60 })
    class Meta:
        abstract = True

# Model for all poster links
class PosterLink(ImageLink):

    #Image sizes
    SIZED_WIDTH =  200
    SIZED_HEIGHT = 263

    THUMB_WIDTH = 50
    THUMB_HEIGHT = 66
Was it helpful?

Solution

Unfortunately, that's not how Python classes work. The code in the class body (including the field constructors) is evaluated when the class is defined. So at the point when the subclass is defined, the field constructors have already been called and those values are locked in.

In addition, the Django ORM doesn't support overriding model fields. The ImageKit fields aren't regular model fields, but since Django doesn't support this feature anyway, it's not something IK supports. (I'm a maintainer.)

So you're either going to have to live with some duplication or delve into the world of metaclasses. Alternatively, you can create a spec class that varies based on the model.

OTHER TIPS

Try Adding this to your Base class (ImageLink)

@classmethod
def showDefaultValue(cls)
print 'default value = %s' % (cls.default)

and then call PosterLink.showDefaultValue()

I would create a function similar to this that returned a dictionary of all the default params for your specific case. This should give you enough of a start though.

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