Search code examples
pythonlektor

Lektor CMS how to get related objects of a model instead of plain list?


I am using Lektor CMS for my blog. I want to implement a categories functionality as said in docs. I have a blog-post model with categories field:

[fields.categories]
label = Категории
type = checkboxes
source = site.query('/categories')

As you notice, I'm using Russian language, so the category name maybe Кодинг and slug (directory name) may be Coding.

The problem is that when i access to the categories of blog-post I get plain list: [u'Coding'] instead of list of objects which I can use to output the name in Russian and generate an url in English, something like /blog/category/coding.

I expect to be able to get such html:

{% for category in post.categories %}
    <a href="{{ category|url }}">{{ category.name }}</a>
{% endif %}

But it is not working. How can I fix this?


Solution

  • Try this:

    {% for category in post.categories %}
        {% set cat = pad.query('/blog/category').filter(F.name == category).first() %}
        <a href="{{ cat|url }}">{{ cat.name }}</a>
    {% endfor %}