Sunday, March 1, 2015

Re: Different between Object-save and form-save

OK, thanks for the response. So the essence of the form_save() is to get the user object in which the password property can be set properly.
Thanks alot

On Mar 1, 2015 6:38 PM, "Vijay Khemlani" <vkhemlan@gmail.com> wrote:
Both save the user to the database, but "user_form.save()" leaves the user password in plain text (which won't work when the user tries to login later) so the password must be set correctly (user.set_password(user.password)) and then the user has to be saved again for the correct (hashed) password to be stored.

On Sun, Mar 1, 2015 at 12:57 PM, ADEWALE ADISA <solixzsystem@gmail.com> wrote:

Hi guys, pls help with little explanation on the extract below from tango tutorial. In the code, they call user_form().save() to save into database. Then latter call user.save(). Pls what's the different between the two ? Where did user.save() save to ?

from rango.forms import UserForm, UserProfileForm

def register(request):

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        # If the two forms are valid...
        if user_form.is_valid() and profile_form.is_valid():
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = profile_form.save(commit=False)
            profile.user = user

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMGzuy_saD%3DscV_CJtKzcss2siPktvFxi1oCUD-BvCEURtA5jg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALn3ei34SRTYAxP4oaSExM2Nt-tjym4PXP2q2xuSgkkH8TV_UA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMGzuy8aX8ehs4Qd2RgkueERA-OmDQg8gbE%3DVxJCB8heKw%2B03A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment