【问题标题】:Django, Cannot assign None, does not allow null valuesDjango,不能分配无,不允许空值
【发布时间】:2009-11-27 21:54:09
【问题描述】:

我有这个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”不允许空值。

可能是什么问题?

【问题讨论】:

    标签: django django-models django-admin


    【解决方案1】:
     #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)
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-22
        • 2017-09-21
        • 2011-07-14
        • 2013-05-11
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多