Tuesday, September 11, 2018

RE: How to get multi form initial data?

You are using a dictionary as your form_class.  It should only be one form, as far as I know.  I am quite interested to learn how you got the form working by specifying a dictionary.

 

Please review documentation:

https://docs.djangoproject.com/en/2.1/topics/class-based-views/generic-editing/

 

And for Classy Class-Based Views:

http://ccbv.co.uk/projects/Django/2.0/django.views.generic.edit/CreateView/

 

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of Django Lover
Sent: Tuesday, September 11, 2018 4:16 AM
To: Django users
Subject: How to get multi form initial data?

 

I have the view like this-

 

The form is working good but i issue is not able to get from initial data. PLEASE HELP

 

**formview.py**

class AddProfile(LoginRequiredMixin, CreateView):

 

    template_name = 'users/add_user_details.html'

   form_class = {

       'contactInformation': ContactInformationForm,

       'additionalInfomation': AdditionalInfomationForm,

       'userDetailsForm': UserDetailsForm,

        'userBusinessDetailsForm':UserBusinessDetailsForm,

   }

   success_url = '/dashboard/'


   
//HERE IS CODE TO GET INITIAL DATA BUT NOT ABLE TO GET IT.

    def get_initial(self):

       if self.request.GET:

           initial = super(AddProfile, self).get_initial()

 

            user = self.request.user

           initial['first_name'] = user.first_name

           initial['last_name'] = user.last_name

           initial['email'] = user.email

 

            return initial

 

    def form_valid(self, form):

        user = self.request.user

 

        # UserDetailsForm = form['userDetailsForm'].save(commit=False)

       userBusinessDetailsForm = form['userBusinessDetailsForm'].save(

           commit=False)

       contactInformation = form['contactInformation'].save(commit=False)

       additionalInfomation = form['additionalInfomation'].save(commit=False)

 

        is_business = self.request.POST.get('business_selection')

        status = False

       if is_business:

           status = True

       else:

           status = False

       

       user.first_name = form['userDetailsForm'].cleaned_data.get('first_name')

       user.last_name = form['userDetailsForm'].cleaned_data.get('last_name')

       user.email = form['userDetailsForm'].cleaned_data.get('email')

       user.save()

 

        # UserDetailsForm.user = user

       # UserDetailsForm.save()

 

        contactInformation.user = user

       contactInformation.save()

 

        contactInformation.business_or_individual = status

       contactInformation.save()

 

        additionalInfomation.user = user

       additionalInfomation.save()

 

        userBusinessDetailsForm.user = user

       userBusinessDetailsForm.save()

 

        messages.add_message(self.request, messages.SUCCESS, _('Profile details has been successfully updated.'))

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

 

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4cef2a2c-52d2-4611-8ca6-af7473adf78c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment