click
V2EX  ›  Django

django-taggit怎么列出某篇文章的tag

  •  
  •   click · Oct 2, 2013 · 4504 views
    This topic created in 4784 days ago, the information mentioned may be changed or developed.
    django-taggit官方的使用说明
    >>> apple = Food.objects.create(name="apple")
    >>> apple.tags.add("red", "green", "delicious")
    >>> apple.tags.all()
    [<Tag: red>, <Tag: green>, <Tag: delicious>]
    >>> apple.tags.remove("green")
    >>> apple.tags.all()
    [<Tag: red>, <Tag: delicious>]
    >>> Food.objects.filter(tags__name__in=["red"])
    [<Food: apple>, <Food: cherry>]
    ----------------------------------
    到了这一步,怎么取出tag,列出apple,cherry这些标签
    8 replies  •  1970-01-01 08:00:00 +08:00
    2code
    2code
    Oct 2, 2013 via iPad
    1
    something.tags.all() 不就是吗
    click
    click
    OP
    Oct 2, 2013
    2
    @2code 我想要red、green、delicious,不是[<Tag: red>, <Tag: green>, <Tag: delicious>]
    wangchen
    wangchen
    Oct 2, 2013❤️1
    3
    问题很奇怪,是要 tag.name 的 list 么?

    name_list = [ t.name for t in apple.tags.all() ]
    wenbinwu
    wenbinwu
    Oct 2, 2013
    4
    .tags.values_list('name', flat=True)
    ?
    tioover
    tioover
    Oct 2, 2013
    5
    for something.tags.all(): print(...)
    tioover
    tioover
    Oct 2, 2013❤️1
    6
    打错了
    for i in something.tags.all(): print(i.[???])
    click
    click
    OP
    Oct 2, 2013
    7
    @wangchen You right!
    Thanks
    wangchen
    wangchen
    Oct 3, 2013
    8
    @click U r welcome.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   822 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:25 · PVG 02:25 · LAX 10:25 · JFK 13:25
    ♥ Do have faith in what you're doing.