Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can use form_class with extra fields #43

Closed
flaudizio opened this issue Aug 2, 2016 · 4 comments
Closed

How can use form_class with extra fields #43

flaudizio opened this issue Aug 2, 2016 · 4 comments

Comments

@flaudizio
Copy link

Hi,

with betterforms can possible to use form_classes with more one field (like extra from inlineformset_factory) ?

Thank's a lot.

@rockymeza
Copy link
Contributor

Hi @flaudizio,

Are you talking about MultiForm?

You can use an a FormSet inside of a MultiForm. There is an example of using an inline formset inside of a MultiForm in the tests: https://github.com/fusionbox/django-betterforms/blob/master/tests/forms.py#L129-L146

Is that what you were thinking?

@flaudizio
Copy link
Author

Hi, thank's a lot.

One else thing, when I save in view I get this error:

can't set attribute

multiform.py in is_valid:

       try:
           cleaned_data = self.clean()
       except ValidationError as e:
           self.add_crossform_error(e)
       else:
           if cleaned_data is not None:
               for key, data in cleaned_data.items():
                   self.forms[key].cleaned_data = data

...

       return forms_valid and not self.crossform_errors
   def non_field_errors(self):
       form_errors = (
           form.non_field_errors() for form in self.forms.values()
           if hasattr(form, 'non_field_errors')

My forms.py

class ContactForm(ModelForm):
class Meta:
model = Contact
fields = ['name']

class PhoneForm(ModelForm):
class Meta:
model = Phone
fields = ['number']

PhoneInlineFormSet = inlineformset_factory(Contact, Phone,
fields=('number',), extra=3)

class ContactPhoneForm(MultiModelForm):
form_classes = {
'contato': ContactForm,
'telefone': PhoneInlineFormSet,

}

def __init__(self, *args, **kwargs):
    instance = kwargs.pop('instance', None)
    if instance is not None:
        kwargs['instance'] = {
            'contact': instance,
            'phone': instance,
        }
    super(ContactPhoneForm, self).__init__(*args, **kwargs)

In my views.py

class ContactCreate(CreateView):
form_class = ContatoTelefoneForm
template_name = "forms/contato.html"
success_url = reverse_lazy('home')
success_message = "Contato cadastro com sucesso."

def form_valid(self, form):

    contato = form['contact'].save()
    telefone = form['phone'].save(commit=False)

    telefone.contato = contato
    telefone.save()

    return super(ContactCreate, self).form_valid(form)

def get_success_url(self):
    redirect_to = self.success_url
    if not is_safe_url(url=redirect_to, host=self.request.get_host()):
        redirect_to = self.success_url
    return redirect_to

Once again, very thank you.

Atenciosamente,
Flaudizio B. S. Filho
82.8809.4382 (OI)
82.9662.3482 (TIM)

On Tue, Aug 2, 2016 at 11:16 AM, Rocky Meza notifications@github.com
wrote:

Hi @flaudizio https://github.com/flaudizio,

Are you talking about MultiForm?

You can use an a FormSet inside of a MultiForm. There is an example of
using an inline formset inside of a MultiForm in the tests:
https://github.com/fusionbox/django-betterforms/blob/master/tests/forms.py#L129-L146

Is that what you were thinking?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#43 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHDA39tNsbeIu8hK_-WRByPE4yAHDbgtks5qb1EygaJpZM4JakZS
.

@rockymeza
Copy link
Contributor

Oh darn, I think you are running into the #41 bug. I'm going to close this bug in favor of that one. I think perhaps I have a solution for it in the #44 PR.

@flaudizio
Copy link
Author

Hi,

can you show the example using inline ?

I just haven't this (in view):

AttributeError
'list' object has no attribute 'contato'

contato = form['contato'].save()
telefone = form['telefone'].save(commit=False)
telefone.contato = contato
telefone.save()

Thank's and sorry for work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants