2017-06-08 71 views
0

我想將登錄用戶的數據設置爲概要文件表單。爲什麼不在下面的視圖中填充模型表單?使用自定義查詢集填充FormView

class UpdateProfile(FormView): 
    model = User 
    form_class = ProfileForm 

    def get_queryset(self): 
     return self.model.objects.get(pk=self.request.user.id) 

class ProfileForm(forms.ModelForm): 
    class Meta: 
     model = User 
     fields = ['email', 'name', 'company', 'title'] 

我的所有字段都是空的。

回答

1

對於單個對象視圖,您需要覆蓋get_object並使用UpdateView

所以爲了獲取登錄的用戶,你可以這樣做:

from django.views.generic import UpdateView 

class UpdateProfile(UpdateView): 
    model = User 
    form_class = ProfileForm 

    def get_object(self): 
     return self.request.user 
+1

這是美麗的。人們如何不喜歡CBV? –