python django数据库,使用了两张表,
一张文章列表,
一张文章内容表
文章内容表使用了OneToOneField,在数据更改的时候,如何保存?
文章列表设计
文章内容表设计
视图:
报错信息:
python django数据库,使用了两张表,
一张文章列表,
一张文章内容表
文章内容表使用了OneToOneField,在数据更改的时候,如何保存?
文章列表设计
文章内容表设计
视图:
报错信息:
:
class ArticleList(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
pub_date = models.DateTimeField('date published')
文章内容表设计:
class ArticleContent(models.Model):
article = models.OneToOneField(ArticleList, on_delete=models.CASCADE)
content = models.TextField()
更改数据时:
# 更改文章列表
article_list = ArticleList.objects.get(id=1)
article_list.title = 'new title'
article_list.save()
# 更改文章内容
article_content = ArticleContent.objects.get(article=article_list)
article_content.content = 'new content'
article_content.save()