Created
March 19, 2014 15:09
-
-
Save PabloVallejo/9643740 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# views.py | |
class UserProfileUpdate(LoginRequiredMixin, UpdateView): | |
# ... | |
# Pass current user to form through `initial` | |
def get_form_kwargs(self, **kwargs): | |
kwargs = super(UserProfileUpdate, self).get_form_kwargs(**kwargs) | |
kwargs['initial']['user']=self.request.user | |
return kwargs | |
# ... | |
# forms.py | |
class UserUpdateForm(forms.ModelForm): | |
# ... | |
def __init__(self, *args, **kwargs): | |
# Getting the user calling the parent save method, | |
# might trigger an unhandled error. | |
# | |
# user = super(UserUpdateForm, self).save(commit=False) | |
# | |
# So, we could get the current user from the `initial` attrbute | |
# set in the `views.py`. | |
self.user = self.initial['user'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment