开发者

Passing arguments to ForeignKey class

开发者 https://www.devze.com 2023-04-09 07:52 出处:网络
I am using stdimage to auto resize my images, I have something like that : class Image(models.Model):

I am using stdimage to auto resize my images, I have something like that :

class Image(models.Model):
    image = stdimage.StdImageField(upload_to="images/uploads/", size=(width, height))


class Nevermind(models.Model):
    w, h = 300, 150
    image = models.ForeignKey(Image)

class Nevermind2(models.Model):
    w, h = 100, 100
    image = models.ForeignKey(Image)

So I would like pass width 开发者_开发百科and heigth as arguments to the Image class.

Thanks


I'm not sure exactly what your goal is, but you could try using model methods to define extra paramenters with the models.

#model
class Nevermind2(models.Model):
    image = models.ForeignKey(image)

    def size(self):
        w,h = 100,100
        return w, h

#example use
>>> a = Nevermind2.objects.get(pk=1)
>>> a.size()
(100,100)
>>> w,h = a.size()
>>> w
100
>>> h
100
0

精彩评论

暂无评论...
验证码 换一张
取 消