5
我在主幹上使用django 1.3,並開始瞭解類視圖。用戶在新的Django類視圖窗體上的當前用戶
我有這樣的:
class Project(models.Model):
title = models.CharField(max_length=90)
slug = models.CharField(max_length=90)
description = models.TextField()
owner = models.ForeignKey(User)
class ProjectForm(ModelForm):
class Meta:
model = Project
exclude = ('owner',)
widgets = {
'description': Textarea(attrs={'cols': 40, 'rows': 20}),
}
class ProjectCreateView(CreateView):
model = Project
form_class = ProjectForm
def get(self, request, *args, **kwargs):
return super(ProjectCreateView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return super(ProjectCreateView, self).post(request, *args, **kwargs)
我試圖使用模型的形式從Django中排除所有者領域,隨着request.user屬性後,獲取字段值以得到一種形式。
表格就這樣工作。但是如何確保ModelForm使用request.user作爲Project.owner字段?
我的第一個猜測,是從視圖
class ProjectCreateView(CreateView):
# .....
def get_form_kwargs(self, **kwargs):
self.initial['owner': self.request.user})
kwargs = super(ProjectCreateView, self).get_form_kwargs(**kwargs)
return kwargs
覆蓋get_form_kwargs方法,但似乎對由模型形式
當我拿到不知道怎麼做T他正確使用當前的django中繼版本,我用補丁製作了一張票來解決這個問題→http://code.djangoproject.com/ticket/14940 – 2010-12-22 18:22:01