开发者

Django formset set current user

开发者 https://www.devze.com 2023-02-19 08:55 出处:网络
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 reference

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"]
            student.save()
        return UserWarning

then in views.py

@login_required
def new_student(request):
    from django.forms.formsets import formset_factory
    try:
        if request.method == 'GET':
            newStudentFormset = formset_factory(forms.NewStudentForm, extra=2)
            formset = newStudentFormset()
            return shortcuts.render_to_response('NewStudent.html', { 'newStudentFormSet':formset, 'active_username': request.user.username })
        elif request.method == 'POST':
            if LOGIN_FORM_KEY in request.POST:
                return _handle_login(request)
            data = request.POST.copy()
            newStudentFormset = formset_factory(forms.NewStudentForm)
            formset = newStudentFormset(data) ### Pass current user to formset? ###
            if formset.is_valid():
                formset.save()
                request.user.message_set.create(message="Save successful.")
                return shortcuts.redirect(student)
            else:
                return shortcuts.render_to_response('NewStudent.html', { 'newStudentFormSet':formset, 'active_username': request.user.username, 'error_message':formset.errors})
        return http.HttpResponseNotAllowed(['GET', 'POST'])
    except models.Student.DoesNotExist:
        return http.HttpResponseNotFound('<h1>Requested Student not found</h1>')


By adding a class that extends BaseFormSet you can add custom code to pass a parameter to the form.

in forms.py:

class NewStudentFormSet(BaseFormSet):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(NewStudentFormSet, self).__init__(*args, **kwargs)

    def _construct_forms(self): 
        self.forms = []
        for i in xrange(self.total_form_count()):
            self.forms.append(self._construct_form(i, user=self.user))

Then in views.py:

# ...

data = request.POST.copy()
newStudentFormset = formset_factory(forms.NewStudentForm, formset=forms.NewStudentFormSet)
formset = newStudentFormset(data, user=request.user)

# ...

Thanks to Ashok Raavi.


I rather to iterate forms directly in the view:

for form in formset.forms:
    form.user = request.user
    formset.save()
  • It avoid creating unecessary BaseFormSet
  • It is cleaner


Based on Paulo Cheque answer (which didn't really work for my case).

I loved the idea of not writing a custom BaseFormSet inherited class.

if formset.is_valid():
    new_instances = formset.save(commit=False)
    for new_instance in new_instances:
        new_instance.user = request.user
        new_instance.save()


I tried the solution of selfsimilar but the BaseFormSet didn't work in my Django 1.6.

I followed the steps in: https://code.djangoproject.com/ticket/17478 and the way that worked for me is:

class NewStudentFormSet(BaseFormSet):
        def __init__(self, *args, **kwargs):
            self.user = kwargs.pop('user',None)
            super(NewStudentFormSet, self).__init__(*args, **kwargs)
            for form in self.forms:
                form.empty_permitted = False

        def _construct_forms(self):
            if hasattr(self,"_forms"):
                return self._forms
            self._forms = []
            for i in xrange(self.total_form_count()):
                self._forms.append(self._construct_form(i, user=self.user))

            return self._forms

        forms = property(_construct_forms)


Here is a similar question about passing form parameters to a formset:

Django Passing Custom Form Parameters to Formset

Personally, I like the second answer on there about building the form class dynamically in a function because it is very fast to implement and easy to understand.

0

精彩评论

暂无评论...
验证码 换一张
取 消