当我使用django taggit保存它们时,我试图为我的帖子对象添加一些标签。所以我尝试了这个
def panties():
from lxml import html
pan_url = 'http://www.ideos.org'
shtml = requests.get(pan_url, headers=headers)
soup = BeautifulSoup(shtml.text, 'html5lib')
video_row = soup.find_all('div', {'class': 'video'})
name = 'pan videos'
author = User.objects.get(id=1)
tags = Post.tags.add("Musica", "video") <------here
def youtube_link(url):
youtube_page = requests.get(url, headers=headers)
soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
video_row = soupdata.find_all('script', {'type': 'text/javascript'})
entries = [{'text': str(div),
} for div in video_row]
tubby = str(entries[4])
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
return urls
def embed(url):
new_embed = url.replace("watch?v=", "embed/")
return new_embed
entries = [{'href': div.a.get('href'),
'src': youtube_link(div.a.get('href'))[1],
'text': div.h4.text,
'comments': div.h4.text,
'name': name,
'url': div.a.get('href'),
'embed': embed(youtube_link(div.a.get('href'))[0]),
'author': author,
'video': True,
'tag': tags <------here
} for div in video_row][:13]
但是这给了我错误信息
Can't call add with a non-instance manager
所以我接着尝试了这个
def panties():
from lxml import html
pan_url = 'http://www.ideos.org'
shtml = requests.get(pan_url, headers=headers)
soup = BeautifulSoup(shtml.text, 'html5lib')
video_row = soup.find_all('div', {'class': 'video'})
name = 'pan videos'
author = User.objects.get(id=1)
post = Post() <------created instance
tags = post.tags.add("Musica", "video")
def youtube_link(url):
youtube_page = requests.get(url, headers=headers)
soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
video_row = soupdata.find_all('script', {'type': 'text/javascript'})
entries = [{'text': str(div),
} for div in video_row]
tubby = str(entries[4])
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
return urls
def embed(url):
new_embed = url.replace("watch?v=", "embed/")
return new_embed
entries = [{'href': div.a.get('href'),
'src': youtube_link(div.a.get('href'))[1],
'text': div.h4.text,
'comments': div.h4.text,
'name': name,
'url': div.a.get('href'),
'embed': embed(youtube_link(div.a.get('href'))[0]),
'author': author,
'video': True,
'tag': tags
} for div in video_row][:13]
它给了我这个错误消息
发布对象需要具有主键值才能访问其标记。我怎样才能做到这一点?我甚至可以做什么?任何帮助将不胜感激。
答案 0 :(得分:2)
您正在尝试向帖子管理器添加标签。 您想要将标签添加到Post实例。
您需要一个已保存的Post实例才能添加标记
e.g。
post_instance = Post.objects.get_or_create(foo=bar)
tags = post_instance.tags.add("Musica", "video")
答案 1 :(得分:0)
借助@luke_aus的回答,我明白了。这正是我的代码现在对于那些可能想要了解如何实际执行此操作的人的看法。它就是从这里开始的。
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()
到这个
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()
希望这有助于某人