Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
Getting a total number of objects inside each object using django markup

Using Django, I am trying to output the total number of lots in each community.

A community has several lots, and there are several communities. Here is an example of what I am getting:

Total lots: 99. 

This community only has 2 lots. There are 4 communities with a total of 9 lots between them.

The models and views seem correct. Is there a filter I am missing or a different way of writing this to get the correct result?

{% if community.is_active %}
<a class="panel-link" href="{% url 'community-detail' pk=community.id %}" %}>
    <div class="col-md-6 community_col">
        <div class="community_box">
            <div class="row">
                <br>
                <div class="col-md-4 community_img">
                    <img src="/media/{{ community.logo }}" alt="">
                </div>
                <div class="col-md-7 col-md-offset-1">
                    <h1 style="margin-bottom: -10px; margin-top: -15px;"><small>{{ community.name }}</small></h1>
                    <h4>{{ community.city }}, {{ community.state }}</h4>
                    <h6>Total lots: {% for lot in community.lot_set.all %}{{ lots|length }}{% endfor %}</h6>
                    <h6>Total Active lots: </h6>
                    <h6>Total Sold lots: </h6>
                    <h6>Total Inactive lots: </h6>
                </div>
            </div>
        </div>
    </div>
</a>
{% endif %}
{% endfor %}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This logic:

{% for lot in community.lot_set.all %}{{ lots|length }}{% endfor %}

Iterates through each lot object. You then take the length of lots which as far as I can tell is not a variable in context. If you wanted to just count the objects:

{{ community.lot_set.count }}

would do.

Looking ahead though, you're going to want to get counts of Active, Sold, Inactive, etc, and to do this efficiently you should look into annotating the queryset and doing this counting in the database:

https://docs.djangoproject.com/en/1.11/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda