问题
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:
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) ?
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