0

Hi guys,

I have this models:

class Pais(models.Model):
    nome = models.CharField('Nome', max_length=50)


class Brinq(models.Model):
    descricao = models.CharField('Nome', max_length=50)

class Filhos(models.Model):
    nome = models.CharField('Nome', max_length=50)
    idade = models.IntegerField('Idade')
    pai = models.ForeignKey('Pais')
    brinq = models.ForeignKey('Brinq', related_name='Brinq')

This view:

def editPai(request, idpai=None):
    if idpai:
        pai = Pais.objects.get(id=idpai)
    else:
        pai = None

    ItensInlineFormSet = inlineformset_factory(Pais, Filhos, form=FilhosForm, extra=1)
    formPais = PaisForm()
    formsetItens = ItensInlineFormSet(instance=pai)

    return render_to_response("base.html", {
        "formPais": formPais,  "formsetItens": formsetItens
    }, context_instance=RequestContext(request), )

and this forms:

class PaisForm(ModelForm):
    class Meta:
        model = Pais

class FilhosForm(ModelForm):
    class Meta:
        model = Filhos

Ok, How can I get "descricao" value from "Brinq" model in my template? I think it's a simple question but, I tried looking, looking and looking again from internet and I don't find anything about this.

I start to thing it's not possible to do it using django, I want to believe that I'm wrong, but as I said, I didn't find anything about this in internet.

I try:

 {%  for form in formsetItens %}
  <tr>
       <td> {{ form.nome }}</td>
       <td> {{ form.idade }}</td>
       <td> {{ form.brinq__descricao }}</td>
  </tr>
  {% endfor %}

and {{ form.brinq.descricao}} to, and nothing... :(

Can anyone help me with this problem?

Regards,

fh_bash
  • 1,725
  • 4
  • 16
  • 23
  • I don't understand your question. If this is a `Pais` form you can't access to `brinq.descricao` because don't exists the relation `pais.brinq`. A `Pais` has several binded `Brinq` models through n:m `Filhos` relation. Also, you can test with `form.instance.your_related_model.some_property`. – dani herrera Aug 22 '13 at 14:52
  • @danihp I try to access `Filhos` model, that has `brinq` FK field to `Brinq` model. My problem is, How can I access `brinq` value using `Filhos` model? Like in my example, I have `formsetItens` is a inlineformset from `Filhos`. I think the django when got the query to get all my `Filhos` values, django put some JOIN to get all related field os `Filhos` model. – fh_bash Aug 22 '13 at 15:12

1 Answers1

0

You are trying to iterate over a FormSet. As the docs say "The formset gives you the ability to iterate over the forms in the formset and display them as you would with a regular form".

So you can for example do the following to display all the fields included in the form:

{%  for form in formsetItens %}
    {{ form.as_table }}
{% endfor %}

..or if it fits your use case you could wrap each form into a form tag, and loop over the form fields:

{%  for form in formsetItens %}
    <form action="/contact/" method="post">
        {% for field in form %}
            <div class="fieldWrapper">
                {{ field.errors }}
                {{ field.label_tag }} {{ field }}
            </div>
        {% endfor %}
        <p><input type="submit" value="Send message" /></p>
   </form>
{% endfor %}
Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177
  • My problem isn't how to show `Filho` form. I try to access Filhos model, that has brinq FK field to Brinq model. My problem is, How can I access brinq value using Filhos model? Like in my example, I have formsetItens is a inlineformset from Filhos. I think the django when got the query to get all my Filhos values, django put some JOIN to get all related field os Filhos model – fh_bash Aug 22 '13 at 15:14
  • If you only want to access a related field of a model you should not use FormSets. FormSets are, as the name say, used to handle forms. – Thomas Kremmel Aug 22 '13 at 15:25
  • Ok, but I need to hangle forms and show this values. I have one table with several columns, only 3 columns is the form, others I need to get this values only to show for user. How can I do these? – fh_bash Aug 22 '13 at 16:18