Adding Keywords individually

181 views
Skip to first unread message

David Novakovic

unread,
Aug 14, 2012, 10:06:58 AM8/14/12
to mezzani...@googlegroups.com
Hey, I had a poke through the KeywordField source but I couldn't figure out how to add individual keywords to the string/set without messing around with save_form_data.

If I can get the general direction on the best way to implement this I'd be happy to do so..

Cheers!

D

Stephen McDonald

unread,
Aug 14, 2012, 5:12:38 PM8/14/12
to mezzani...@googlegroups.com
Not sure if this is the shorted possible version, but here's a snippet that'll add a tag to a page/blog instance called "obj":

from mezzanine.generic.models import AssignedKeyword, Keyword

keyword, _ = Keyword.objects.get_or_create(title="my keyword")
obj.keywords.add(AssignedKeyword(keyword=keyword))
--
Stephen McDonald
http://jupo.org

David P. Novakovic

unread,
Aug 15, 2012, 9:02:45 AM8/15/12
to mezzani...@googlegroups.com
Great thanks mate. Seems like something that would be nice as a helper
method on the mixin.

Been spoilt by the various tagging libs out there I suppose :)

Stephen McDonald

unread,
Aug 15, 2012, 4:33:26 PM8/15/12
to mezzani...@googlegroups.com
Yeah that's a great idea.

David P. Novakovic

unread,
Nov 22, 2012, 7:39:33 PM11/22/12
to mezzani...@googlegroups.com
I honestly can't remember actually!

I'm pretty sure all of my changes have been accepted into mezzanine proper now. If it's not there yet then I never got to it!

I did find some issues with dupes being added if you don't check them first.

Here is some code verbatim from a now redundant import on a client project (note, I think this works but you should test it first):

                for kw in keywords:
                    kw = kw.strip()
                    if kw:
                        keyword, created = Keyword.objects.get_or_create(title=kw)
                        if kw not in [k.keyword.title for k in recipe_inst.keywords.all()]:
                            recipe_inst.keywords.add(AssignedKeyword(keyword=keyword))

So I'm not sure on the best approach.. I would assume dupes are not desirable, but Stephen will no doubt be able to make an executive decision.

D


On Fri, Nov 23, 2012 at 10:32 AM, Nathan Keller <i...@madteckhead.com> wrote:
+1 this was a bit counter intuitive having worked with django-tagging and django-taggit before. Did you come up with a helper David?

(Loving Mez btw)

Stephen McDonald

unread,
Nov 22, 2012, 7:41:53 PM11/22/12
to mezzani...@googlegroups.com
Definitely don't want dupes - I have a weird suspicion "add" handles this somehow, but I'm probably wrong.

I don't recall either of us adding anything for this either. Would make a nice contribution :-)
Reply all
Reply to author
Forward
0 new messages