本文介绍了Django save methode在模型更改时与信号冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以较小的分辨率保存后盖和对应的后盖缩略图.最终用户还可以更改后盖.为了完成适当的文件处理,请使用model.signals.在我尝试更改Post对象的postcover之前,这似乎工作得很好.

I want to save a postcover and a correspondending postcover thumbnail in a smaller resolution.In the end users can also change the postcover. To accomplish a proper file handling im working with model.signals. this seems to work pretty fine until i try to change the postcover of my Post object.

当我尝试使用更改的postcover保存Post对象时,出现以下错误:

When i try to save the Post object with a changed postcover i get the following error:

 ValueError: The 'postcover' attribute has no file associated with it.

initiali创建和删除Post对象的工作正常.

initiali creating and deleting the Post object works as expected.

models.py

models.py

class Post(models.Model):
...
    postcover = models.ImageField(
        verbose_name="Post Cover",
        blank=True,
        null=True,
    )
    postcover_tn = models.ImageField(
        verbose_name="Post Cover Thumbnail",
        blank=True,
        null=True,
    )
...

def save(self, *args, **kwargs):
    super(Post, self).save(*args, **kwargs)
    if self.postcover:
        if os.path.exists(self.postcover.path):
            image = Image.open(self.postcover)
            outputIoStream = BytesIO()
            baseheight = 600
            hpercent = baseheight / image.size[1]
            wsize = int(image.size[0] * hpercent)
            imageTemproaryResized = image.resize((wsize, baseheight))
            imageTemproaryResized.save(outputIoStream, format='PNG')
            outputIoStream.seek(0)
            self.postcover = InMemoryUploadedFile(outputIoStream, 'ImageField',
                                                  "%s.png" % self.postcover.name.split('.')[0], 'image/png',
                                                  sys.getsizeof(outputIoStream), None)

            image = Image.open(self.postcover)
            outputIoStream = BytesIO()
            baseheight = 100
            hpercent = baseheight / image.size[1]
            wsize = int(image.size[0] * hpercent)
            imageTemproaryResized = image.resize((wsize, baseheight))
            imageTemproaryResized.save(outputIoStream, format='PNG')
            outputIoStream.seek(0)
            self.postcover_tn = InMemoryUploadedFile(outputIoStream, 'ImageField',
                                                  "%s.png" % self.postcover.name.split('.')[0], 'image/png',
                                                  sys.getsizeof(outputIoStream), None)
    elif self.postcover_tn:
        self.postcover_tn.delete()

    super(Post, self).save(*args, **kwargs)

signals.py

signals.py

@receiver(models.signals.post_save, sender=Post)
def post_auto_delete_files_on_change(sender, instance, **kwargs):
    """
    Deletes old file from filesystem
    when corresponding object is updated
    with new file.
    """
    if not instance.pk:
        return False

    try:
        old_postcover = sender.objects.get(pk=instance.pk).postcover
        old_postcover_tn = sender.objects.get(pk=instance.pk).postcover_tn
    except sender.DoesNotExist:
        return False
    if not old_postcover:
        return False

    new_postcover = instance.postcover
    if not old_postcover == new_postcover:
        if os.path.isfile(old_postcover.path):
            os.remove(old_postcover.path)
            os.remove(old_postcover_tn.path)

forms.py上还有一个postcover删除选项,可能会影响此过程:

I also have a postcover delete option at my forms.py which might influence this process:

        if self.instance.postcover:
            self.fields['remove_cover'].disabled = False
            self.fields['postcover'].label = mark_safe('Overwrite Cover:')
            self.fields['remove_cover'].label = mark_safe('Remove Cover:')
        else:
            self.fields['postcover'].label = mark_safe('Cover:')
            del self.fields['remove_cover']


    def save(self, commit=True):
        instance = super(PostForm, self).save(commit=False)
        if self.cleaned_data.get('remove_cover'):
            try:
                os.unlink(instance.postcover.path)
            except OSError:
                pass
            instance.postcover = None
        if self.cleaned_data.get('remove_attachment'):
            try:
                os.unlink(instance.postattachment.path)
            except OSError:
                pass
            instance.postattachment = None
        if commit:
            instance.save()
        return instance

推荐答案

您应使用 post_save 信号.由于您使用的是 pre_save 现在,您的信号处理程序在对象保存到数据库之前调用.将您的代码更改为此:

You should use post_save signal. Since you are using pre_save now, your signal handler called before object saving to DB. Change your code to this:

@receiver(models.signals.post_save, sender=Post)
def post_auto_delete_files_on_change(sender, instance, **kwargs):

这篇关于Django save methode在模型更改时与信号冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 23:36