如何通过在 Django 中拆分标题来创建文章标签
How to create Article tags from splitting the Title in Django
我想根据 RSS 提要的标题创建文章标签 post。然后将标签保存到数据库中,标题为 post_id 我同时从中获取了标签。像这样:
Title = "Voyant raises M to scale production of its tiny, inexpensive lidar tech"
Tags = ['Voyant', 'Raises', 'M', 'To', 'Scale', 'Production', 'Of', 'Its', 'Tiny', 'Inexpensive', 'Lidar', 'Tech']
假设 post_id 为 1,标签 table 应如下所示:
id | tag | post_id
--------------------------------
1 | Voyant | 1
2 | Raises | 1
我的 table(来源、帖子和标签)中有 3 个模型。
class Source(models.Model):
name = models.CharField(max_length=500, verbose_name='Website Name')
class Posts(models.Model):
post_title = models.CharField(max_length=500, verbose_name='Post Title')
source = models.ForeignKey(Source, on_delete=models.CASCADE, verbose_name='Source')
class Tags(models.Model):
name = models.CharField(max_length=500)
post = models.ForeignKey(Posts, on_delete=models.CASCADE, verbose_name='Posts')
到目前为止,我已经能够拆分上面的标题了。
title = item.title
strip_away = title.replace(",", "").replace(":", "").replace("(", "").replace(")", "").replace("'", "").replace("[", "").replace("]", "").replace("!", "").replace("?", "").replace("-", " ")
capital = strip_away.title()
article_tags = capital.split()
但是现在我的问题出现在保存部分。
def fetch_articles():
feed = feedparser.parse("my_site_of_preference")
source = Source.objects.get(pk=1)
source_id = int(source.pk)
source_name = source
save_new_articles(source_id, source_name, feed)
def save_new_articles(source_id, source_name, feed):
selected_source_id = source_id
for item in feed.entries:
title = item.title
""" The splitting code """
if not Posts.objects.filter(post_title=title).exists():
post = Posts(post_title = title, source_id = selected_source_id)
post.save()
for i in range(len(article_tags)):
tags = Tags.objects.create(name = article_tags[i], post_id = source_name.pk)
tags.save()
我一直收到错误消息:
django.db.utils.IntegrityError: insert or update on table "Posts_tags" violates foreign key constraint "Posts_tags_post_id_3e6ae939_fk_Posts_posts_id"
DETAIL: Key (post_id)=(1) is not present in table "Posts_posts".
post还没有保存来创建一个post_id,保存标签的时候可以作为PK使用。保存 post 标题后我该如何保存标签?
保存标签时,您应该用对象引用 post,而不是它的 pk。 Django ORM 会为你做这件事。使用 create 时,不需要再次保存,因为 create() 已经保存了。在循环中尝试以下操作:
Tags.objects.create(name=article_tags[i], post=post)
我想根据 RSS 提要的标题创建文章标签 post。然后将标签保存到数据库中,标题为 post_id 我同时从中获取了标签。像这样:
Title = "Voyant raises M to scale production of its tiny, inexpensive lidar tech"
Tags = ['Voyant', 'Raises', 'M', 'To', 'Scale', 'Production', 'Of', 'Its', 'Tiny', 'Inexpensive', 'Lidar', 'Tech']
假设 post_id 为 1,标签 table 应如下所示:
id | tag | post_id
--------------------------------
1 | Voyant | 1
2 | Raises | 1
我的 table(来源、帖子和标签)中有 3 个模型。
class Source(models.Model):
name = models.CharField(max_length=500, verbose_name='Website Name')
class Posts(models.Model):
post_title = models.CharField(max_length=500, verbose_name='Post Title')
source = models.ForeignKey(Source, on_delete=models.CASCADE, verbose_name='Source')
class Tags(models.Model):
name = models.CharField(max_length=500)
post = models.ForeignKey(Posts, on_delete=models.CASCADE, verbose_name='Posts')
到目前为止,我已经能够拆分上面的标题了。
title = item.title
strip_away = title.replace(",", "").replace(":", "").replace("(", "").replace(")", "").replace("'", "").replace("[", "").replace("]", "").replace("!", "").replace("?", "").replace("-", " ")
capital = strip_away.title()
article_tags = capital.split()
但是现在我的问题出现在保存部分。
def fetch_articles():
feed = feedparser.parse("my_site_of_preference")
source = Source.objects.get(pk=1)
source_id = int(source.pk)
source_name = source
save_new_articles(source_id, source_name, feed)
def save_new_articles(source_id, source_name, feed):
selected_source_id = source_id
for item in feed.entries:
title = item.title
""" The splitting code """
if not Posts.objects.filter(post_title=title).exists():
post = Posts(post_title = title, source_id = selected_source_id)
post.save()
for i in range(len(article_tags)):
tags = Tags.objects.create(name = article_tags[i], post_id = source_name.pk)
tags.save()
我一直收到错误消息:
django.db.utils.IntegrityError: insert or update on table "Posts_tags" violates foreign key constraint "Posts_tags_post_id_3e6ae939_fk_Posts_posts_id"
DETAIL: Key (post_id)=(1) is not present in table "Posts_posts".
post还没有保存来创建一个post_id,保存标签的时候可以作为PK使用。保存 post 标题后我该如何保存标签?
保存标签时,您应该用对象引用 post,而不是它的 pk。 Django ORM 会为你做这件事。使用 create 时,不需要再次保存,因为 create() 已经保存了。在循环中尝试以下操作:
Tags.objects.create(name=article_tags[i], post=post)