简体   繁体   English

Django模型中的对象总数

[英]Total count of objects in Django Model

Using Django ~=1.11 and Python 3.6 使用Django〜= 1.11和Python 3.6

I am a beginner! 我是初学者! Every answer I've found online for my question is more advanced than what I'm looking for. 我在网上针对问题找到的每个答案都比我要寻找的答案更高级。

Here's my model: 这是我的模型:

class Byte(models.Model):
    text = models.CharField(max_length=30)

    def __str__(self):
        return self.text  

Here's my view: 这是我的看法:

def byte_list(request):
    bytes = Byte.objects.order_by('text')
    return render(request, 'cloudapp/byte_list.html', {'bytes': bytes})

Here's my template: 这是我的模板:

{% block content %}
    <div class="total">
        <h2>Total number of words and phrases entered: {{ byte.count }}</h2>
    </div>
<hr>
{% for byte in bytes %}
    <div class="byte">
        <h2>{{ byte.text }}</h2>
    </div>
{% endfor %}
{% endblock %}

This allows the creation "Byte" objects in the /admin, with only one field - a small text field. 这允许在/ admin中创建“字节”对象,该对象只有一个字段-一个小的文本字段。 Right now, the template simply displays a list of all the objects created. 现在,模板仅显示所有已创建对象的列表。

Question/Problem: I'd like to display the total number/count of objects that have been created for the Byte model. 问题/问题:我想显示为Byte模型创建的对象总数。 In the template, I have a tag {{ byte.count }} to display this. 在模板中,我有一个标签{{byte.count}}来显示它。

I've tried using count() and Aggregation, but not sure how to work those into my model/view/template. 我试过使用count()和Aggregation,但不确定如何将它们用于模型/视图/模板。 I'm looking for the most simple and up-to-date way to accomplish this, whether it's using a method or @property in the model, or some type of query set in the view. 我正在寻找最简单,最新的方法来完成此操作,无论是在模型中使用方法还是@property,还是在视图中使用某种类型的查询集。

def byte_list(request):
    byte= Byte.objects.count()
    bytes = Byte.objects.order_by('text')
    return render(request, 'cloudapp/byte_list.html', {'bytes': bytes,'byte':byte})

And in template 并在模板中

 {{ byte }}

You've got a few different options... the most common ways to get the total number of model instances I have seen are: 您有几种不同的选择...获取我见过的模型实例总数的最常用方法是:

my_total = len(Byte.objects.filter())

or, without having to run the full query: 或者,而不必运行完整的查询:

my_total = Byte.objects.count()

Here's a link to the resource doc for 1.11: https://docs.djangoproject.com/en/1.11/topics/db/aggregation/#cheat-sheet 以下是指向1.11的资源文档的链接: https : //docs.djangoproject.com/en/1.11/topics/db/aggregation/#cheat-sheet

There's nothing wrong with Exprator's answer, but one alternative is to use the built in length template filter: Exprator的答案没有错,但是一种选择是使用内置的length模板过滤器:

<h2>Total number of words and phrases entered: {{ bytes|length }}</h2>

If you're not planning to iterate over the bytes queryset you could also call count on it directly in the template: 如果您不打算遍历bytes ,则也可以直接在模板中对其调用count

<h2>Total number of words and phrases entered: {{ bytes.count }}</h2>

That will force a second database query, though, so only do that if you aren't otherwise causing bytes to be evaluated. 但是,这将强制执行第二次数据库查询,因此,只有在不以其他方式导致要评估bytes才可以这样做。

The decision of what to put in the view and what to do with template filters/no-arg methods is more a question of style than a hard and fast rule. 决定在视图中放置什么以及如何使用模板过滤器/无参数方法的决定更多的是样式问题,而不是一成不变的规则。 Erring on the side of using the view is usually right, here it's simple enough that I might just do it in the template. 使用视图方面的错误通常是正确的,在这里它很简单,我可以在模板中进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django model 对象的计数问题 - Django Problem with count of model objects 如何计算 Django 中的模型对象 - How to count model objects in Django 使用django-haystack计算模板中的总搜索对象数 - Count total search objects count in template using django-haystack 如何在 django 中获取 model 对象的计数 - How to get count of model objects in django Django REST框架仅返回分页和总计数所需的对象 - Django REST Framework Return Only Objects Needed For Pagination & Total Count 如何计算Django中相同模型的不同字段的总出现次数? - How to count total occurrence of distinct field of same model in django? 如何获得 Django 模型的多对多字段的总数? - How to get total count of many to many field to a Django model? 如何获取模型的相关对象和模型的子项相关对象的总计数? - How can I get a total count of a model's related objects and the model's children's related objects? Django通过属性与另一个QuerySet的交集计数对模型对象进行排序 - Django sort model objects by intersection count of a property with another QuerySet 如何在 django 中使用“自我”/自己的 model 获取 ManyToManyField 中的对象计数? - How to get count of objects in a ManyToManyField with 'self' / its own model in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM