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

164
Visualizações
Difference between formset and formset.forms in django template tags

I'm trying to loop through forms in a formset on my template. And I have seen two different ways of doing this and it doesn't seem to make a difference to my code which one I use.

{{ formset.management_form }}
    {% for form in formset %}
        {{ form }}
        {% endfor %}

And...

{{ formset.management_form }}
    {% for form in formset.forms %}
        {{ form }}
        {% endfor %}

Does this make any difference? Why put .forms on the end?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

According to the source of the BaseFormset class:

def __iter__(self):
    """Yields the forms in the order they should be rendered"""
    return iter(self.forms)

@cached_property
def forms(self):
    """
    Instantiate forms at first property access.
    """
    # DoS protection is included in total_form_count()
    forms = [self._construct_form(i, **self.get_form_kwargs(i))
             for i in range(self.total_form_count())]
    return forms

Both methods (for form in formset and for form in formset.forms) are identical.

You see, the __iter__ which is used for the for loop yields each time the self.forms. On the other hand, the for form in formset.forms iterates over the same thing, the self.forms.

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