通过反向关系注释 django 查询

发布于 2024-09-12 19:44:02 字数 1421 浏览 6 评论 0原文

我有两个模型 PropertyImage,它们通过外键相关,这样每个 Property 都有多个 Image 实例。我正在尝试检索所有属性的查询集 - 列出所有字段 - 以及每个属性拥有的图像数量。我知道我可以将其作为两个不同的查询来执行,但我不认为这是一种特别优雅的方法,并且事实证明效率有点低,因为此信息是通过 XMLHttpRequest 检索的。

模型定义如下:

class Property(models.Model):
    title = models.CharField('title', max_length=255)
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)

    class Meta:
        pass


class Image(models.Model):
    prop_id = models.ForeignKey(Property)
    image_file = models.ImageField('image file', upload_to='/path/to/image/')

    class Meta:
        pass

我已遵循此处发布的答案:跨反向关系的 Django 聚合,因为我相信这是一个类似的问题,但我发现这返回一个空查询集。

感谢任何人可以提供的任何帮助。

编辑:

我运行的查询是:

Property.objects.all().annotate(image_count=Count('image')).order_by('-image_count')

编辑2:

经过一些实验,我找到了一个解决方案,尽管我很确定这符合错误/非-已记录的问题:

Property.objects.all().annotate(Count('image')).order_by('-image__count')
Property.objects.all().annotate(total_images=Count('image')).order_by('-total_images')

这两者都有效,但命名注释 image_count 却不起作用。如果不深入研究 Django 源代码,我无法真正推测为什么会发生这种情况。

I have two models Property and Image, related by foreign key such that each Property has multiple instances of Image. I'm trying to retrieve a queryset of all the properties - listing all fields - and the number of images that each property has. I am aware that I could do this as two distinct queries, but I don't feel that this is a particularly elegant approach, and is proving a little inefficient as this information is being retrieved via XMLHttpRequest.

The models are defined as follows:

class Property(models.Model):
    title = models.CharField('title', max_length=255)
    created = models.DateTimeField('created', auto_now_add=True)
    modified = models.DateTimeField('modified', auto_now=True)

    class Meta:
        pass


class Image(models.Model):
    prop_id = models.ForeignKey(Property)
    image_file = models.ImageField('image file', upload_to='/path/to/image/')

    class Meta:
        pass

I have followed the answer posted here: Django Aggregation Across Reverse Relationship, as I believe this was a similar problem, but I've found that this returns an empty queryset.

Thanks for any help anyone can offer.

EDIT:

The query I ran was:

Property.objects.all().annotate(image_count=Count('image')).order_by('-image_count')

EDIT 2:

After some experimentation, I have found a solution, though I'm pretty sure that this qualifies as a bug / non-documented issue:

Property.objects.all().annotate(Count('image')).order_by('-image__count')
Property.objects.all().annotate(total_images=Count('image')).order_by('-total_images')

These both work, but naming the annotation image_count did not. Without delving into the Django source, I can't really speculate as to why that's happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-09-19 19:44:02

您发布的代码应该可以工作 - 在任何情况下,它都不应该返回空查询集,因为注释不会影响主查询的过滤。

愚蠢的问题,但是您确定数据库中有 Property 元素吗?

The code you've posted should work - in any case, it should not return an empty queryset as annotate doesn't affect the filtering of the main query.

Silly question, but are you sure there are Property elements in the database?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文