Class 基于视图 UpdateView django 不同类型的模型

Class Based Views UpdateView django different type of model

如何为不同类型的用户更改 UpdateView 中的模型?我从 AbstractBaseUser 继承了 StudentTeacher,我需要为它们编辑

class EditUser(UpdateView):
    success_url = '/success/'
    template_name = 'edit-profile.html'
    model = Teacher (I need to choose this Teacher or Student)

我知道 get_template_names(self) 方法或 get_success_url(self),但找不到任何 get_model 方法。 我需要类似的东西:

def get_model_name(self):
    if self.request.user.user_type == 'teacher':
        return Teacher
    if self.request.user.user_type == 'student':
        return Studend

谢谢。

根据 Django documentation:

model

The model that this view will display data for. Specifying model = Foo is effectively the same as specifying queryset = Foo.objects.all(), where objects stands for Foo’s default manager.

queryset

A QuerySet that represents the objects. If provided, the value of queryset supersedes the value provided for model.

get_queryset()

Returns the queryset that will be used to retrieve the object that this view will display. By default, get_queryset() returns the value of the queryset attribute if it is set, otherwise it constructs a QuerySet by calling the all() method on the model attribute’s default manager.

所以,你只需要重新定义get_queryset方法