losee losee - 4 months ago 19
Python Question

I am getting this error using django taggit "Can't call add with a non-instance manager"

I'm trying to add a few tags to my post object when I save them, using django taggit. So I tried this

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]


but that gave me the error message

Can't call add with a non-instance manager


So I then tried this

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]


and it gave me this error message

Post objects need to have a primary key value before you can access their tags. How Can I make this work? is what I'm doing even possible? Any help would be appreciated.

Answer

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

Comments