帮助,我不明白发生了什么。试图删除数据库,仍然出现此错误。看一下代码,告诉我哪里错了。谢谢。

models.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()


表格

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不能为null:

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 - IntegrityErrorquestion_question.pub_date不能为NULL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25672937/

10-09 20:17