KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Related to this question , but expanding on it - How would I use this technique in a formset? I'd like to use the current logged in user in a form, but I'm using the form in a formset. The referenced solution for a single form is to pass request.user to the form and process in init. How do I add to the kwargs for each form in the formset? Example in my code: in forms.py class NewStudentForm (forms.Form): username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$', help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."), error_message = _("This value must contain only letters, numbers and underscores.")) first_name = forms.CharField(label=_('first name'), max_length=30 ) last_name = forms.CharField(label=_('last name'), max_length=30, ) email = forms.EmailField(label=_('e-mail address') ) password = forms.CharField(label=_('password'), max_length=64, ) class Meta: model = User fields = ("username","first_name", "last_name", "email", "password") def __init__(self, *args, **kwargs): self._user = kwargs.pop('user') super(NewStudentForm, self).__init__(*args, **kwargs) def save(self, commit=True): user = super(NewStudentForm, self).save(commit=False) user.set_password(self.cleaned_data["password"]) if commit: user.save() profile = Profile.objects.create_profile(user) profile.activiation_key = profile.ACTIVATED_KEY profile.authorized = True profile.save() user.is_active=True user.save() student = models.Student() student.user = user student.teacher = self._user student.plaintext_pwd = self.cleaned_data["password"] s
Tags (comma-separated)
Save Edits
Cancel