Django formset设置当前用户

时间:2011-03-24 21:52:07

标签: django django-forms django-views

this question相关,但正在扩展 - 我如何在表单集中使用此技术?

我想在表单中使用当前登录的用户,但我在formset中使用该表单。单个表单的引用解决方案是将request.user传递给init中的表单和进程。如何为formset中的每个表单添加kwargs?

我的代码示例:

在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

然后在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>')

5 个答案:

答案 0 :(得分:19)

通过添加扩展BaseFormSet的类,您可以添加自定义代码以将参数传递给表单。

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))

然后在views.py

# ...

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

# ...

感谢Ashok Raavi

答案 1 :(得分:6)

我宁愿直接在视图中迭代表单:

for form in formset.forms:
    form.user = request.user
    formset.save()
  • 避免创建不必要的BaseFormSet
  • 更清洁

答案 2 :(得分:4)

基于保罗检查答案(对我的案例不起作用)。

我喜欢不编写自定义BaseFormSet继承类的想法。

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

答案 3 :(得分:2)

我尝试了自相似的解决方案,但BaseFormSet在我的Django 1.6中没有用。

我按照以下步骤操作:https://code.djangoproject.com/ticket/17478,对我有用的方法是:

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)

答案 4 :(得分:0)

以下是关于将表单参数传递给表单集的类似问题:

Django Passing Custom Form Parameters to Formset

就个人而言,我喜欢关于在函数中动态构建表单类的第二个答案,因为它实现起来非常快且易于理解。