fellipeh

Untitled

Aug 22nd, 2013
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. MODELS:
  2.  
  3. class Pais(models.Model):
  4.     nome = models.CharField('Nome', max_length=50)
  5.  
  6.  
  7. class Brinq(models.Model):
  8.     descricao = models.CharField('Nome', max_length=50)
  9.  
  10. class Filhos(models.Model):
  11.     nome = models.CharField('Nome', max_length=50)
  12.     idade = models.IntegerField('Idade')
  13.     pai = models.ForeignKey('Pais')
  14.     brinq = models.ForeignKey('Brinq', related_name='Brinq')
  15.  
  16.  
  17. VIEW:
  18.  
  19. def editPai(request, idpai=None):
  20.     if idpai:
  21.         pai = Pais.objects.get(id=idpai)
  22.     else:
  23.         pai = None
  24.  
  25.     ItensInlineFormSet = inlineformset_factory(Pais, Filhos, form=FilhosForm, extra=1)
  26.     formPais = PaisForm()
  27.     formsetItens = ItensInlineFormSet(instance=pai)
  28.  
  29.     return render_to_response("base.html", {
  30.         "formPais": formPais,  "formsetItens": formsetItens
  31.     }, context_instance=RequestContext(request), )
  32.  
  33.  
  34.  
  35. FORM:
  36.  
  37. class PaisForm(ModelForm):
  38.     class Meta:
  39.         model = Pais
  40.  
  41. class FilhosForm(ModelForm):
  42.     class Meta:
  43.         model = Filhos
  44.  
  45.  
  46. and simple HTML:
  47.  
  48.  
  49.  {{ formPais }}
  50.  <table>
  51.   {%  for form in formsetItens %}
  52.   <tr>
  53.        <td> {{ form.nome }}</td>
  54.        <td> {{ form.idade }}</td>
  55.        <td> {{ form.brinq__descricao }}</td>
  56.   </tr>
  57.   {% endfor %}
  58.  </table>
Advertisement
Add Comment
Please, Sign In to add comment