Mezzanine BlogCategory parsing categories

时光毁灭记忆、已成空白 提交于 2019-12-14 02:20:10

问题


I want to build a custom filter that takes a blog_post as an argument and does some parsing of the categories (attached to the blog post).

I tried like this:

from mezzanine import template
from mezzanine.blog.models import BlogPost, BlogCategory

register = template.Library()

@register.filter(name='has_friends')
def has_friends(blog_post):
  categories = blog_post.categories.all()
  if 'Friends' in categories:
    return False
  else:
    return True

The problem is that blog_post.categories.all() returns something like this:

[<BlogCategory: Enemies>, <BlogCategory: Allies>, <BlogCategory: Friends>, <BlogCategory: Family>]

Questions:

  1. how can I get the list of categories parsed like this ['Enemies', 'Allies', 'Friends', 'Family'] instead of above (in order for my if statement to work) ?

  2. without the answer at above question 1, how can I use IF statement to search in the BlogCategory list shown above?

Thank you,

GG


回答1:


Found the answer myself, this way: I used dir(category) to get its methods => found among: title, slug, etc... then I use:

for category in categories:
    if category.title == 'Friends':
        # do stuff


来源:https://stackoverflow.com/questions/29403335/mezzanine-blogcategory-parsing-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!