首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IntegrityError at /contact NULL约束失败: first_contact.fname

IntegrityError at /contact NULL约束失败: first_contact.fname
EN

Stack Overflow用户
提问于 2021-05-06 12:31:04
回答 1查看 498关注 0票数 1

在运行此代码时获取完整性错误。另外,在对models.datefield()进行了一些修改之后,它进行得很好,但是数据库没有存储条目,在填写表单时表是空的。

models.py

代码语言:javascript
复制
# Create your models here.
class Contact(models.Model):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=122) 
    desc = models.TextField()
    city = models.CharField(max_length=20)
    phone = models.CharField(max_length=12) 
    date = models.DateField()

这是我的views.py

代码语言:javascript
复制
from datetime import datetime
from first.models import Contact
# Create your views here.
def index(request):
    return render(request, 'index.html')
    # return HttpResponse("This is our home page")

def about(request):
     return render(request, 'about.html')
    #return HttpResponse("This is our about page")

def services(request):
     return render(request, 'services.html')
     #return HttpResponse("This is our services page")
    

def contact(request):
     if request.method == "POST":
          fname  = request.POST.get('fname')
          lname  = request.POST.get('lname')
          desc  = request.POST.get('desc')
          city  = request.POST.get('city')
          phone  = request.POST.get('phone')
          contact = Contact(fname=fname, lname=lname, desc=desc, city=city, phone=phone, 
 date=datetime.today())
          contact.save()
     return render(request, 'contact.html')
     #return HttpResponse("This is our contact page")
EN

回答 1

Stack Overflow用户

发布于 2021-05-06 13:27:38

您正在尝试将datetime对象分配给DateFieldDateField只接受date对象。

datetime.today()仍然返回一个datetime对象,但是时间将被设置为所有的零,以指示一天的开始。如果您真的不关心时间的存储,请使用date.today()。如果要存储时间信息,请将字段转换为DateTimeField

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67418196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档