限制CreateView中的表单字段,以便不将当前用户显示为模板

时间:2016-06-02 14:11:14

标签: python django dictionary django-views django-class-based-views

我使用简单的表单和通用的CBV UpdateView给所有用户使用“令牌”来为其他用户提供额外的功劳。不应该允许用户给自己额外的信用,因此他们不应该在选择字段中显示为选择。我没有在这里使用模型表单,并且想知道如何在不这样做的情况下执行此操作。这是我到目前为止的代码:

models.py

class ExtraCredit(models.Model):
    recipient = models.ForeignKey(Developer, related_name='extracredit_recipient')
    sender = models.ForeignKey(Developer, related_name='extracredit_sender')
    skill = models.ForeignKey(Skill, related_name='extracredit_skill')
    description = models.TextField(blank=True, null=True)
    date_credited = models.DateField(auto_now_add=True)

    def __str__(self):
        return '%s %s -> %s %s - %s' % (self.sender.user.first_name, self.recipient.user.last_name, self.recipient.user.first_name, self.recipient.user.last_name, self.skill.name)

views.py

class ExtraCreditCreateView(CreateView):
    model = ExtraCredit
    template_name = 'extracredit_create.html'

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

        self.fields['recipient'].queryset = ExtraCredit.objects.exclude(recipient=user)
        self.fields['description'].help_text = "Why does this user deserve extra credit?"

    def get_form_kwargs(self):
        kwargs = super(ExtraCreditCreateView, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def get_success_url(self):
        return reverse('my_developer_details')

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))

    def form_valid(self, form):
        form.save(commit=False)
        sender = get_object_or_404(Developer, user_id=self.request.user.id)
        extra_credit_tokens = Developer.objects.get(user_id=sender.user_id).extra_credit_tokens

        if extra_credit_tokens:
            Developer.objects.filter(user_id=sender.user_id).update(extra_credit_tokens=extra_credit_tokens-1)

            form.instance.sender = sender
            form.save()

            return super(ExtraCreditCreateView, self).form_valid(form)
        else:
            raise forms.ValidationError("The user does not have enough tokens")

    fields = ['recipient', 'skill', 'description']

我拥有get_form_kwargs(self)功能的唯一原因是尝试在self.request.user函数中使用def __init__。如果我在__init__函数中使用self.request.user,则会收到一条错误,即请求不是ExtraCreditCreateView的属性。如果我确实向选项添加了请求,例如__init__(self, request, *args, **kwargs):,那么我会收到一个关于需要两个参数而只提供一个参数的错误。现在设置它的方式,我得到一个TypeError:list indices must be integers, not str

在模板中创建表单时,确保当前登录用户不在“收件人”字段中的正确方法是什么?

更新的解决方案(根据@ Alasdair的答案):

class ExtraCreditForm(forms.ModelForm):
    class Meta:
        model = ExtraCredit
        exclude = ['id', 'sender', 'date_credited']

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

        self.fields['recipient'].queryset = self.fields['recipient'].queryset.exclude(user_id=user.id)
        self.fields['description'].help_text = "Why does this user deserve extra credit?"

class ExtraCreditCreateView(CreateView):
    template_name = 'extracredit_create.html'
    form_class = ExtraCreditForm

    def get_form_kwargs(self):
        kwargs = super(ExtraCreditCreateView, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

    def get_success_url(self):
        return reverse('my_developer_details')

    def form_valid(self, form):
        form.save(commit=False)
        sender = get_object_or_404(Developer, user_id=self.request.user.id)
        extra_credit_tokens = Developer.objects.get(user_id=sender.user_id).extra_credit_tokens

        if extra_credit_tokens:
            Developer.objects.filter(user_id=sender.user_id).update(extra_credit_tokens=extra_credit_tokens-1)

            form.instance.sender = sender
            form.save()

            return super(ExtraCreditCreateView, self).form_valid(form)
        else:
            raise forms.ValidationError("The user does not have enough tokens")

1 个答案:

答案 0 :(得分:3)

您说您想避免定义模型表单,但这是最好的方法。 可能可以在get_form方法中破解表单的字段,但你不应该这样做。

设置收件人的查询集属于表单,而不是视图的__init__方法。

class ExtraCreditForm(forms.ModelForm):

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

        self.fields['recipient'].queryset = ExtraCredit.objects.exclude(recipient=user)
        self.fields['description'].help_text = "Why does this user deserve extra credit?"

然后从您的视图中移除__init__方法,并设置form_class以使其使用您的模型表单。您已更新get_form_kwargs以将用户转到表单__init__方法,因此您不必进行任何其他更改。您的form_invalid方法没有做任何特别的事情,因此您可以删除它。

class ExtraCreditCreateView(CreateView):
    form_class = ExtraCreditForm
    ...