Django,不能分配None,不允许空值

时间:2009-11-27 21:54:10

标签: django django-models django-admin

我有这个models.py

import datetime
from django.db import models
from tinymce import models as tinymce_models
from filebrowser.fields import FileBrowseField

class ItemWithMedia(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

class Actual(ItemWithMedia):
    published = models.DateField('Published')
    title_hr = models.CharField('(hr)', max_length=200)
    title_en = models.CharField('(en)', max_length=200)
    body_text_hr = models.TextField('(hr)')
    body_text_en = models.TextField('(en)')

    def __unicode__(self):
        return self.title_hr

    class Meta:
        verbose_name = "Aktualno"
        verbose_name_plural = "Aktualni"
        ordering = ['-published']

当我尝试在管理站点中创建新项目时出现此错误: 无法分配无:“Actual.published”不允许空值。

可能是什么问题?

2 个答案:

答案 0 :(得分:17)

 #for sql 'now()' value use
 published = models.DateField('Published', auto_now_add=True)
 #to allow sql null
 published = models.DateField('Published', null=True, blank=True)

答案 1 :(得分:5)

您需要将参数“null = True,blank = True”添加到已发布的定义中,这样它就不会在数据库中创建为NOT NULL列:

published = models.DateField('Published', null=True, blank=True)