Django:更改 parent,当 child 被删除时(单向 OneToOne)

Django: Change the parent, when the child get deleted(unidirectional OneToOne)

我想在删除 LabeledPhoto object 时将 Photo 'labeled' 字段更改为 False。

#models.py
class Photo(models.Model):
    image = models.ImageField(upload_to='shoes_data/%Y/%m/%d', name='image')
    created = models.DateTimeField(auto_now_add=True)
    labeled = models.BooleanField(default=False)

class LabeledPhoto(models.Model):
    labeled_image = models.OneToOneField(Photo, on_delete=models.PROTECT, related_name='labeled_image')
    topcategory = models.CharField(max_length=64)
    subcategory = models.CharField(max_length=64)
    labeler = models.CharField(max_length=32)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

我这样试过,但是没用

# views.py
class LabeledPhotoDelete(DeleteView):
    model = LabeledPhoto
    template_name = 'label/labeled_photo_delete.html'
    success_url = reverse_lazy('photo:labeled_list')

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        success_url = self.get_success_url()
        labeled = LabeledPhoto.objects.get(id=self.object.pk)
        labeled.labeled_image.labeled = False
        labeled.save()
        self.object.delete()
        return reverse(success_url)

您需要 .save() Photo:

from django.http import HttpResponseRedirect

class LabeledPhotoDelete(DeleteView):
    model = LabeledPhoto
    template_name = 'label/labeled_photo_delete.html'
    success_url = reverse_lazy('photo:labeled_list')

    def delete(self, request, *args, **kwargs):
        self.object = self.get_object()
        success_url = self.get_success_url()
        image = self.object.labeled_image
        image.labeled = False
        <strong>image.save()</strong>
        self.object.delete()
        return HttpResponseRedirect(success_url)

但是没有理由在字段中存储图像是否被标记:如果您想知道这一点,您可以简单地查找它。实际上,您可以添加 属性:

class Photo(models.Model):
    image = models.ImageField(upload_to='shoes_data/%Y/%m/%d', name='image')
    created = models.DateTimeField(auto_now_add=True)

    <b>@property</b>
    def <b>labeled</b>(self):
        try:
            return self.labeled_image is not None
        except LabeledPhoto.DoesNotExist:
            return False

或者如果 LabeledPhoto 存在则用事实注释查询集:

from django.db.models import BooleanField, ExpressionWrapper, Q

Photo.objects.annotate(
    is_labeled=ExpressionWrapper(
        Q(<strong>labeled_image__isnull=False</strong>),
        output_field=BooleanField()
    )
)