Home > Software design >  How to create Article tags from splitting the Title in Django
How to create Article tags from splitting the Title in Django

Time:12-31

I want to create articles tags from the title of a RSS feed post. Then save the tags into a DB with a post_id of the title i got the tags from at the same time. Something like this:

Title = "Voyant raises $15M to scale production of its tiny, inexpensive lidar tech"
Tags = ['Voyant', 'Raises', '$15M', 'To', 'Scale', 'Production', 'Of', 'Its', 'Tiny', 'Inexpensive', 'Lidar', 'Tech']

Assuming the post_id is 1, the Tags table should look like:

id    |    tag     |   post_id
--------------------------------
 1    |  Voyant    |      1
 2    |  Raises    |      1

I have 3 models in my table(Source, Posts & Tags).

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')

So so far i was able to split the title above.

title = item.title
strip_away = title.replace(",", "").replace(":", "").replace("(", "").replace(")", "").replace("'", "").replace("[", "").replace("]", "").replace("!", "").replace("?", "").replace("-", " ")
capital = strip_away.title()
article_tags = capital.split()

But now my problem comes during the saving part.

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()

I keep getting the error:

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".

The post hasn't been saved to create a post_id that it can be used as a PK when saving the tags. How can i go about this to save the tags after saving the post title?

CodePudding user response:

When saving tags, you should reference the post with the object, not the pk of it. Django ORM will do that for you. And when using create, you do not need to save it again as create() already saves it. Try the following within your loop:

Tags.objects.create(name=article_tags[i], post=post)
  • Related