如何在UpdateView中访问SelectDateWidget?

时间:2017-08-19 03:30:51

标签: django

如何在基于类的通用视图中的表单中实现类似于SelectDateWidget的东西?

例如:

class UserUpdateView(LoginRequiredMixin, UpdateView):

    fields = ['birthday',]

    model = User

    def get_success_url(self):
        return reverse('users:detail',
                       kwargs={'username': self.request.user.username})

    def get_object(self):

        return User.objects.get(username=self.request.user.username)

1 个答案:

答案 0 :(得分:2)

您可以使用form_class来执行此操作,例如:

# .....
from django import forms
from django.forms.widgets import SelectDateWidget


class UserUpdateForm(forms.Form):
    birthday = forms.DateField(widget=SelectDateWidget())


class UserUpdateView(LoginRequiredMixin, UpdateView):
    model = User
    fields = ['birthday',]
    form_class = UserUpdateForm