【问题标题】:Django get count of photos by gallery beside gallery nameDjango 按画廊名称旁边的画廊获取照片数量
【发布时间】:2015-07-15 07:00:22
【问题描述】:

构建类似于照片库的 Django 1.8 应用程序(我的画廊被称为“喷泉”)。我有一个包含喷泉列表的页面,我想在喷泉名称旁边显示照片的数量,例如:

Fountain 1 (49 photos)
Fountain 2 (43 photos)
etc

我了解 Django 有注释来聚合和计算我用来创建以下内容的记录:

 photo_count = Fountains.objects.all().annotate(Count('photos'))

但是,我无法确定该放在哪里。该文档仅显示查询,但并未将查询和结果计数置于应用程序的上下文中。

我在创建喷泉列表数据的视图中完成了这个“photo_count”聚合。

但我的问题是,如何将其放入模板中,以便在喷泉名称旁边显示?

我想我必须将其加载到模板中,然后将其与喷泉 pk 关联?

任何关于 Django 方法是将这种聚合从视图中获取到模板的方法的任何指针都将不胜感激!

我的模型:

class Fountains(models.Model):
    foun_name = models.CharField(max_length=100, blank=True, null=True)
    foun_desc = models.TextField(blank=True, null=True)
    foun_address = models.CharField(max_length = 100, blank=True, null=True)
    foun_lat = models.CharField(max_length=20, blank=True, null=True)
    foun_lon = models.CharField(max_length=20, blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    #tags = TaggableManager()
    #def __unicode__(self):
    #    return self.foun_name.name

    class Meta:
        managed = True
        db_table = 'fountains'
        #verbose_name_plural = 'Fountains'

class Photos(models.Model):
    #filename = models.CharField(max_length=100)
    filename = models.ImageField(upload_to='.')
    ext = models.CharField(max_length=5, blank=True, null=True)
    desc = models.TextField(blank=True, null=True)
    #fountain_id = models.IntegerField(blank=True, null=True)
    fountain = models.ForeignKey(Fountains)
    user_id = models.IntegerField(blank=True, null=True)
    tag_count = models.IntegerField(blank=True, null=True)
    photos_tag_count = models.IntegerField(blank=True, null=True)
    comment_count = models.IntegerField(blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    #def __unicode__(self):
    #    return self.filename.name

    class Meta:
        managed = True
        db_table = 'photos'
        #verbose_name_plural = 'Photos'

我创建喷泉列表的观点:

@login_required
def fountains(request):
    assert isinstance(request, HttpRequest)
    fountain_list = Fountains.objects.order_by('foun_name')
    photo_count = Fountains.objects.all().annotate(Count('photos'))
    context_dict = {'fountains': fountain_list }
    return render(
        request,
        'app/fountains.html',
        context_dict,
        context_instance = RequestContext(request)
    )

我的显示视图结果的模板:

{% extends "app/layout.html" %}

{% block content %}

    <h1>Fountains</h1>
    <p><a href="/fountain_add/">Add Fountain</a></p>
    <ul>
    {% for fountain in fountains %}
        <li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }}</a></li>
    {% endfor%}
    </ul>

{% endblock content %}

【问题讨论】:

    标签: python django


    【解决方案1】:
    fountain_list = Fountains.objects.annotate(Count('photos')).order_by('foun_name')
    
    
    <li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }} ({{ fountain.photos__count  }})</a></li>
    

    【讨论】:

    • 嘿,这太棒了。美观紧凑。但我必须添加 photo_count 别名,如下所示: fountain_list = Fountains.objects.annotate(photo_count = Count('photos')).order_by('foun_name')认为您打算在模板中使用 photo_count 时添加别名?还是我还缺少其他东西?谢谢!
    • 是的,你是对的,我编辑了答案。将是photos__count或需要添加别名annotate(photo_count=Count('photos'))
    • 是的,我明白了,没有别名的 {{ balloon.photos__count }} 也可以。双下划线的意义是什么?我喜欢这个别名,因为它更容易阅读,但双下划线更紧凑。
    【解决方案2】:

    在你的 html 中

    <h1>Fountains</h1>
        <p><a href="/fountain_add/">Add Fountain</a></p>
        <ul>
        {% for fountain in fountains %}
            <li><a href="/photos/{{ fountain.id }}/"}>{{ fountain.foun_name }} ( {{ fountain.photos.all.count }} )</a></li>
        {% endfor%}
        </ul>
    

    【讨论】:

    • {{ 喷泉.照片.count }} 不起作用。如果这些是相关模型,我可以看到它会如何。它应该工作吗?
    • 试试这个:({{喷泉.photos.all.count }})
    • 好的,你很接近。我做了一些挖掘,发现 Django 做了一些你建议的事情。所以这行得通: {{ 喷泉.photos_set.count }} 这个方法似乎是最有效的,我不需要在视图中做任何额外的事情,例如我可以使用原始查询 fountain_list = Fountains.objects.order_by('foun_name') 谢谢
    【解决方案3】:

    在您创建的context_dict 中应该放置您的photo_count

    context_dict = {'fountains': fountain_list, 'photo_count': photo_count}
    

    然后您可以在您的模板中正常使用它。 此外,在不了解您的系统如何工作的情况下,我将从您的 Fountain 模型中的属性重命名所有 foun_ 前缀。

    【讨论】:

    • 我无法让它工作。我将 photo_count 添加到 context_dict 并尝试在模板中使用它,例如 {{ photo_count }} 但我用对象块代替了数字。
    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多