Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-如何摆脱多次保存_Django_Django Models - Fatal编程技术网

Django-如何摆脱多次保存

Django-如何摆脱多次保存,django,django-models,Django,Django Models,我有下面的型号 class Post(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField(max_length=255) description = models.TextField(null=True, blank=True) author = models.ForeignKey(Us

我有下面的型号

class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    image = models.ImageField(max_length=255, upload_to='posts/images/', null=True, blank=True)
    thumbnail = models.FilePathField(path=settings.MEDIA_ROOT, max_length=255, null=True, blank=True)
如您所见,我需要两个图像,用户上传的原始图像和我将在主页上使用的缩略图版本

问题是,我正在使用以下代码在save方法上创建缩略图:

def save(self, *args, **kwargs):
    super(Post, self).save(*args, **kwargs)
    # Get the thumbnail from the image
    if self.image:
        self.thumbnail = get_thumbnail(self.image, '500x500', crop='center', quality=85).url
    super(Post, self).save(*args, **kwargs)
我无法摆脱第一个
super(Post,self)。save(*args,**kwargs)
,因为我希望
self.image
可用,而我无法摆脱第二个
super(Post,self)。save(*args,**kwargs)
,因为这样缩略图就不会被保存

我敢肯定一定有别的办法

你能给我一些建议吗?

使用QuerySet的方法

class Post(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    image = models.ImageField(max_length=255, upload_to='posts/images/', null=True, blank=True)
    thumbnail = models.FilePathField(path=settings.MEDIA_ROOT, max_length=255, null=True, blank=True)

    def save(self, *args, **kwargs):
        super(Post, self).save(*args, **kwargs)
        # Get the thumbnail from the image
        if self.image:
            thumbnail = get_thumbnail(self.image, '500x500', crop='center', quality=85).url
            Post.objects.filter(pk=self.pk).update(thumbnail=thumbnail)
class Post(models.Model):
id=models.UUIDField(主键=True,默认值=uuid.uuid4,可编辑=False)
title=models.CharField(最大长度=255)
description=models.TextField(null=True,blank=True)
author=models.ForeignKey(用户,在_delete=models.CASCADE上,相关的_name='posts')
image=models.ImageField(最大长度为255,上传到='posts/images/',null=True,blank=True)
缩略图=models.FilePathField(路径=settings.MEDIA\u ROOT,最大长度=255,null=True,blank=True)
def保存(自身、*args、**kwargs):
super(Post,self).save(*args,**kwargs)
#从图像中获取缩略图
如果自我形象:
缩略图=获取缩略图(self.image,'500x500',crop='center',quality=85)。url
Post.objects.filter(pk=self.pk).update(thumbnail=thumbnail)

注意:更新过程应该在
if…
子句中:)

如果不调用第一个
超级保存
,则可以将第一个
保存
替换为
自我.image.save(self.image.file.name,self.image.file.file,save=False)
self.image
字段是否为空?