Background: I want to set up an automated workflow to assign tags to new documents. My current plan is to have a bunch of “Attach tag” actions assigned to a state where each one filters whether its tag applies (based on ocr content). But then I also want to assign something like a TODO tag for documents that did not get any tags automatically. To that end I thought I could escalate the state to some “auto-tagging-done” state after a short while and in that final state I would check whether the document has received any tags.
So I want to set up an action that will do “Attach tag” = “TODO” with the condition that the document does not currently have any tags. The question is how I can filter for tag count. I tried around in the sandbox but {{ document.tags__label }}
doesn’t seem to return anything for a test document that does have multiple tags assigned. When I try {{ document.tags }}
I get this result: tags.Tag.None
.
Am I doing something wrong or is something broken?
Or is there an overall better way to do what I want?
I am using Version 4.4.6 with docker installation.
To get just the number of tags of a document use:
{{ document.tags.count }}
{% if document.tags.count %}
Has tags
{% else %}
No tags
{% endif %}

Thank you very much! 
Maybe as a follow-up question, how would one go about finding this? I even looked through the source code for a bit without success (even though I am a Python developer).
I had also tried {{ document.tags | length }}
which always returned 0.
Thank you very much! 

Maybe as a follow-up question, how would one go about finding this? I even looked through the source code for a bit without success (even though I am a Python developer).
I had also tried {{ document.tags | length }}
which always returned 0.
Mayan has hundreds of models which multiplied by the Django’s template tags, ORM constructs and our own custom tags results in thousand of possible combinations. It is impossible to document them all. Only the most common and direct use cases are added by default into the object explorer.
count
is a queryset construct which changes the actual database query, it is not a Python construct or statement.
A count()
call performs a SELECT COUNT(*)
behind the scenes, so you should always use count()
rather than loading all of the record into Python objects and calling len()
on the result (unless you need to load the objects into memory anyway, in which case len()
will be faster).
length
on the other hand is a template tag that returns the items in a list like Python’s .len()
. However it is not ideal for counting queryset results.
-
len(). A
QuerySet
is evaluated when you call len()
on it. This, as you might expect, returns the length of the result list.Note: If you only need to determine the number of records in the set (and don’t need the actual objects), it’s much more efficient to handle a count at the database level using SQL’s SELECT COUNT(*)
. Django provides a count()
method for precisely this reason.
While not intended or optimized, you could still use length
to count queryset items using:
{{ document.tags.all|count }}
These are just basic explanations of .count
and |length
, now imagine attempting to document all of them, when to use each, and how to combine them.
Thank you again for all the explanations. I didn’t realize how much of this is Django functionality that I won’t find directly in Mayan’s sources. I guess I learned some Django today. 
Now I need to get my automations going and then I definitely need to donate something!