所以我试图使用postgres(postgis)数据库在django中填充模型。我遇到的问题是输入日期时间字段。我写了一个人口脚本,但每次运行它时,都会收到错误django.db.utils.IntegrityError: null value in column "pub_date" violates not-null constraint
。下面的代码显示了我的模型和适用于该表的人口脚本部分。Django DateTimeField用户输入
型号:
class Article(models.Model):
authors = models.ManyToManyField(Author)
location = models.ForeignKey(Location)
article_title = models.CharField(max_length=200, unique_for_date="pub_date")
pub_date = models.DateTimeField('date published')
article_keywords = ArrayField(ArrayField(models.CharField(max_length=20, blank=True), size=8), size=8,)
title_id = models.CharField(max_length=200)
section_id = models.CharField(max_length=200)
,人口脚本:
def populate():
add_article(
id = "1",
article_title = "Obama scrambles to get sceptics in Congress to support Iran nuclear deal",
pub_date = "2015-04-06T20:38:59Z",
article_keywords = "{obama, iran, debate, congress, america, un, republican, democrat, nuclear, isreal}",
title_id = "white-house-scrambles-sceptics-congress-iran-nuclear-deal",
section_id = "us-news",
location_id = "1"
)
def add_article(id, article_title, pub_date, article_keywords, title_id, section_id, location_id):
article = Article.objects.get_or_create(article_title=article_title)[0]
article.id
article.article_title
article.pub_date
article.article_keywords
article.title_id
article.section_id
article.location_id
article.save()
return article
if __name__ == '__main__':
print "Starting Newsmap population script..."
populate()
我搜索适合所有年龄段,但似乎没有解决这个特定的问题。任何帮助非常感谢!
你可以试试pubdate作为日期时间对象,如datetime.datetime(2015,04,06,20,38,59) – tanaydin
你的意思是输入日期时间为'pub_date = datetime.datetime(2015,04,06,20, 38,59)'。我只是试过这个,得到了同样的错误。 – sammy88888888
检查http://stackoverflow.com/questions/5855607/django-datetimefield也许有帮助。 – tanaydin