/notes/add NOT NULL 約束處??出現(xiàn) IntegrityError 失?。?/h1>
1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
您沒(méi)有為模型created
中的字段設(shè)置默認(rèn)值Note
。因此,沒(méi)有填寫(xiě)任何值,因此出現(xiàn)錯(cuò)誤:您可以將參數(shù)auto_now_add=…
[Django-doc]設(shè)置為True
自動(dòng)將其設(shè)置為創(chuàng)建對(duì)象時(shí)的時(shí)間戳:
class Note(models.Model):
? ? # …
? ? created = models.DateTimeField(auto_now_add=True)
通常最好改變.instance表格中的包裹,讓表格成為.save()模型。如果您(稍后)向模型添加多對(duì)多關(guān)系Note,這尤其有用,因?yàn)楸韱我愿该鞯姆绞教幚泶诉壿嫞?/p>
def note_add(request):
? ? if request.method == 'POST':
? ? ? ? form = NoteForm(request.POST)
? ? ? ? if form.is_valid():
? ? ? ? ? ? form.instance.user = request.user
? ? ? ? ? ? form.save()
? ? ? ? ? ? return redirect('/notes')
? ? else:
? ? ? ? form = NoteForm()
? ? context={
? ? ? ? 'form': form,
? ? }
? ? return render(request, 'add.html', context)
- 1 回答
- 0 關(guān)注
- 143 瀏覽
添加回答
舉報(bào)