gpt4 book ai didi

python - IntegrityError Question_question.pub_date 不能为 NULL

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:26 25 4
gpt4 key购买 nike

救命,我不明白发生了什么事。尝试删除数据库,但仍然出现此错误。看一下代码并告诉我哪里错了。谢谢。

模型.py

class Question(models.Model):
title = models.CharField(max_length=120)
content = models.TextField()
pub_date = models.DateTimeField(blank=True, null=False)
author = models.ForeignKey(User)
views = models.IntegerField(default=0)
slug = models.SlugField(max_length=240, blank=True)
tag = TaggableManager()

def __unicode__(self):
return self.title

def save(self, **kwargs):
if not self.pk:
self.date_added = date.today()

if self.title and not self.slug:
self.slug = slugify(self.title)

super(Question, self).save(**kwargs)

views.py 的一部分

def ask(request):
if request.method == 'POST':
form = AskForm(request.POST)
if form.is_valid():
question = form.save(commit=False)
question.author = request.user
question.save()

return redirect('/question/one_question') + str(question.id)
return HttpResponseBadRequest()

forms.py

class AskForm(forms.ModelForm):
tag = forms.CharField(max_length=100, required=False, label='Tags:',
help_text="Write here some tags, example: python, django ")

class Meta:
model = Question
fields = ['title', 'content']
labels = {
'title': 'Title', 'content': 'Text of your question'
}

回溯

 Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/question/ask/

Django Version: 1.6.5
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'question',
'south',
'taggit',
'hitcount')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/karmadorje/QandA/question/views.py" in ask
29. question.save()
File "/home/karmadorje/QandA/question/models.py" in save
28. super(Question, self).save(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
545. force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
573. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _save_table
654. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _do_insert
687. using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
232. return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
1514. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
903. cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
69. return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py" in __exit__
99. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
53. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py" in execute
451. return Database.Cursor.execute(self, query, params)

Exception Type: IntegrityError at /question/ask/
Exception Value: question_question.pub_date may not be NULL

最佳答案

在 Question 类中,您说 pub_date 不能为空:

pub_date = models.DateTimeField(blank=True, null=False)

保存问题时,您尚未设置 pub_date:

question = form.save(commit=False)
question.author = request.user
question.save()

在保存之前尝试设置question.pub_date

关于python - IntegrityError Question_question.pub_date 不能为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com