2👍
✅
You are trying to add tags to the Post manager.
You want to add tags to a Post instance.
You need a saved Post instance before you can add the tags
e.g.
post_instance = Post.objects.get_or_create(foo=bar)
tags = post_instance.tags.add("Musica", "video")
0👍
With the aide of @luke_aus answer I figured it out. this is just what my code looks like now for others who may want to see how to actually do it. It went from this.
for entry in entries:
post = Post()
post.title = entry['text']
title = post.title
if not Post.objects.filter(title=title):
post.title = entry['text']
post.name = entry['name']
post.url = entry['url']
post.body = entry['comments']
post.image_url = entry['src']
post.video_path = entry['embed']
post.author = entry['author']
post.video = entry['video']
post.status = 'draft'
post.save()
to this
for entry in entries:
post = Post()
post.title = entry['text']
title = post.title
if not Post.objects.filter(title=title):
post.title = entry['text']
post.name = entry['name']
post.url = entry['url']
post.body = entry['comments']
post.image_url = entry['src']
post.video_path = entry['embed']
post.author = entry['author']
post.video = entry['video']
post.status = 'draft'
post.save()
saved_post = Post.objects.get(title=title)
tags = saved_post.tags.add("Musica", "video")
post.tags = tags
post.save()
hope this helps someone
- [Answered ]-Django ORM Cannot filter by reversed foreign key
- [Answered ]-Open text file stored as record in Django db
- [Answered ]-Django: Images/Absolute URLS from not displaying in html
- [Answered ]-Manipulating Django form to show the correct validation error(s)
Source:stackexchange.com