2015-08-20 74 views
0

將request.user保存爲django額外視圖。將request.user保存到django額外視圖

該示例來自Django-Extra-Views。似乎正在以示例狀態工作。

https://django-extra-views.readthedocs.org/en/latest/views.html

class OrderCreateView(CreateWithInlinesView): 
    model = Order 
    inlines = [ItemsInline, TagsInline] 

    def get_success_url(self): 
     return self.object.get_absolute_url() 

下都試過了。他們不工作。

def form_valid(self, form): 
    # self.object = form.save() 
    form.instance.owner = self.request.user 
    form.save() 
    return super(OrderCreateView, self).form_valid(form) 

throws error: 
IntegrityError at /add/ 
null value in column "owner_id" violates not-null constraint 

也試過若有人遇到的問題將它添加form.save

def save(self, commit=True): 
    instance = super(OrderCreateView, self).save(commit=False) 
    instance.owner = self.request.user 
    instance.save() 


throws 
'OrderForm' object has no attribute 'request' 

回答

0

,我能得到它通過添加下面的查看工作。

def forms_valid(self, form, inlines): 
    form.instance.owner = self.request.user 
    return super(CreateView, self).forms_valid(form, inlines) 
相關問題