Charles Smith
Charles Smith

Reputation: 3289

Query Django Wagtail Document Model

I have a client that has hundreds of documents that are tagged that we need to query and list on a page. I want to write a TemplateTag so its more reusable, but I have no idea how to query the builtin Wagtail image and document models. The below code is what I am starting with Document.objects.all() added for placement only. Any help would be appreciated.

@register.inclusion_tag(
    'tags/_document_snippets.html', takes_context=True
)
def document_snippets(context):
    documents = Documents.objects.all()
    return {
        'documents': documents,
        'request': context['request'],
    }

Upvotes: 0

Views: 534

Answers (1)

Charles Smith
Charles Smith

Reputation: 3289

So I thought I would answer my own question for the benefit of others.

import get_document_model

from wagtail.wagtaildocs.models import get_document_model

create tag

@register.inclusion_tag(
    'tags/_documents_snippets.html',
    takes_context=True
)
def document_snippets(context):
    Document = get_document_model()
    documents = Document.objects.all()
    return {
        'documents': documents,
        'request': context['request'],
    }

add to template

{% for doc in documents %}
    <a href="{{ doc.url }}">{{ doc.title }}</a>
{% endfor %}

Upvotes: 1

Related Questions