3 回答

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超6個(gè)贊
您是否嘗試使用 data['teacher'] = request.user 而不是 data['teacher'] = request.user.username ?

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
我很欣賞這些答案。我設(shè)法通過將表單轉(zhuǎn)換為模型表單來解決這個(gè)問題,因?yàn)樵摫韱文P透m合這種情況,因?yàn)槟P妥侄沃苯佑成涞捷斎搿?/p>
新形式.py
from homework.models import Homework
from django import forms
class HomeworkForm(forms.ModelForm):
class Meta:
model = Homework
fields = ['title', 'description', 'due']
widgets = {
'due': forms.DateInput(format=('%m/%d/%Y'), attrs={'label': 'due date', 'type': 'date'}),
}
添加回答
舉報(bào)