小编Ode*_*ded的帖子

Django QuerySet .count() 为 0 并且 .exists() 为 false,即使 QuerySet 中有一个对象(Django Rest Framework)

几个月来我一直有一个间歇性的错误,我被卡住了!帮助表示赞赏。

class ContentFile(models.Model):
    """
    Represents the metadata for a single document.
    """
    name = models.CharField(max_length=200)
    set = models.ForeignKey(Set, related_name='contentfile_set', on_delete=models.CASCADE)
    location = models.FileField(upload_to=get_content_file_upload_path)

    class Meta:
        app_label = 'ProtocolManager'
        unique_together = ('set', 'location')

    def __str__(self):
        return f"File {self.name} at location {str(self.location)} attached to {self.set}"


@receiver(models.signals.post_delete, sender=ContentFile)
def delete_file(sender, instance, *args, **kwargs):
    """
    Delete orphaned file once last ContentFile is removed.
    """
    log.info(f"Checking {instance.location} for existing ContentFile")
    if instance.location:
        from django.db import connection
        from django.db import reset_queries
        reset_queries()
        files_queryset = ContentFile.objects.filter(location=instance.location)
        if …
Run Code Online (Sandbox Code Playgroud)

python django amazon-web-services amazon-rds django-rest-framework

10
推荐指数
1
解决办法
244
查看次数