django覆盖模型保存方法错误

时间:2016-09-13 15:42:53

标签: django django-models

我有一个包含ImageField的模型。我想从该ImageField中自动创建另外两个字段。

class Images(models.Model):
    ...
    img = models.ImageField(upload_to='img')
    #for autocreating 
    rimg = models.ImageField(upload_to='rimg', null=True, blank=True)
    limg = models.ImageField(upload_to='limg', null=True, blank=True)

    def create_rimg(self):
        # some work for generating rimg
        self.rimg.save()

    def create_limg(self):
        # some work for generating limg
        self.limg.save()

    def save(self, *args, **kwargs):
        self.create_rimg()
        self.create_limg()
        force_update = False

        if self.id:
            force_update = True

        super(Images, self).save(force_update=force_update, *args, **kwargs)

该代码给出500错误。如果我调用该代码,它就完美无缺:

def save(self, *args, **kwargs):
        self.create_rimg()
        #self.create_limg() 
        force_update = False

def save(self, *args, **kwargs):
        #self.create_rimg()
        self.create_limg() 
        force_update = False

如何在save()方法中调用2个函数?

0 个答案:

没有答案