Browse Source

Cria detalhe com pai no crud mestre detalhe

pull/341/head
Marcio Mazza 9 years ago
committed by LeandroRoberto
parent
commit
bac198c059
  1. 8
      crud/masterdetail.py
  2. 4
      parlamentares/tests/test_parlamentares.py

8
crud/masterdetail.py

@ -44,6 +44,14 @@ class MasterDetailCrud(Crud):
def get_url_regex(cls):
return r'^(?P<pk>\d+)/%s/create$' % cls.model._meta.model_name
def get_form(self, form_class=None):
form = super(MasterDetailCrud.CreateView,
self).get_form(form_class)
field = self.model._meta.get_field(self.crud.parent_field)
parent = field.related_model.objects.get(pk=self.kwargs['pk'])
setattr(form.instance, self.crud.parent_field, parent)
return form
class DetailView(CrudDetailView):
@classmethod

4
parlamentares/tests/test_parlamentares.py

@ -70,7 +70,7 @@ def test_dependente_submit(client):
mommy.make(Partido, pk=32)
mommy.make(TipoDependente, pk=3)
client.post(reverse('parlamentares:parlamentar_dependente',
client.post(reverse('parlamentares:dependente_create',
kwargs={'pk': 14}),
{'nome': 'Eduardo',
'tipo': 3,
@ -86,7 +86,7 @@ def test_dependente_submit(client):
@pytest.mark.django_db(transaction=False)
def test_form_errors_dependente(client):
mommy.make(Parlamentar, pk=14)
response = client.post(reverse('parlamentares:parlamentar_dependente',
response = client.post(reverse('parlamentares:dependente_create',
kwargs={'pk': 14}),
{'salvar': 'salvar'},
follow=True)

Loading…
Cancel
Save