如何在基于类的通用视图中的表单中实现类似于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)
答案 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