Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edited... or better... replaced! :)</p> <p>That's true the authors without document matching won't be in the queryset so you will have to add them back after (I couldn't find a better way but maybe someone knows how to not remove them without using raw sql).</p> <p>You get the full documents count of the authors because you don't use the queryset to get the document counts:</p> <pre><code>qryset = Author.objects.all() qryset = qryset.filter(documents__categories__name__in = category_list).distinct() qryset = qryset.filter(documents__date_published__year=year) </code></pre> <p><code>print(qryset)</code> gives <code>[&lt;Author: Author object&gt;]</code> (if only 1 author matched all categories) and <code>qryset[0].documents.count()</code> will return <strong>only the number of documents matched</strong> (not all documents from the author - 2 in the case I tested and the author had 4 but only 2 matching all conditions).</p> <p>If you use dict (.values()) instead of querysets in the steps above, you can't do that (I think) because dict won't have a documents field so:</p> <pre><code>qryset_dict = Author.objects.values() qryset_dict = qryset_dict.filter(documents__categories__name__in = category_list).distinct().values() qryset_dict = qryset_dict.filter(documents__date_published__year=year).values() </code></pre> <p>when you issue <code>qryset_dict[0].documents.count()</code> you receive an error:</p> <pre><code>AttributeError: 'dict' object has no attribute 'documents' </code></pre> <p><br />Now to add the filtered authors back you can do:<br /></p> <pre><code>res = [] for a in Author.objects.all(): if a in qryset: res.append([a,a.documents.count()]) else: res.append([a,0]) </code></pre> <p>and res will be a list with <code>&lt;authors&gt;</code> in 1st column and count of documents matching in 2nd column.</p> <p>I know this is far from perfect... but if you are interested only in the <code>count()</code> of matching documents per author, I think you could find a better way using <a href="http://docs.djangoproject.com/en/1.2/topics/db/aggregation/" rel="nofollow noreferrer">django aggregation and annotation</a> or possibly make it with raw sql using a <code>left join</code> from authors to documents.</p> <p><br /><hr /><br /> EDIT after Clarification in Question:</p> <pre><code>def possible_view(request, categories=None, year=None): # you will pass these as parmeters of course category_list = ['c2', 'c3'] year = 2010 qryset = Document.objects.filter(categories__name__in = category_list).distinct() qryset = qryset.filter(date_published__year=year) authors = Author.objects.all() return render_to_response('blar.html', { 'result': qryset, 'authors': authors, 'categories': category_list, 'year': year }, RequestContext(request)) </code></pre> <p>Template <code>blar.html</code>:</p> <pre><code>{% for a in authors %} &lt;b&gt;{{a.name}}&lt;/b&gt;&lt;br /&gt; {% for d in result %} {% if d.author.name == a.name %} {{ d.title }} is almost certainly in one of these categories: {{ categories }} and was absolutely published in {{ year }}. If not, .something_magical_happens_to_documents didn't work.&lt;br /&gt; {% endif %} {% endfor %}&lt;br /&gt; {% endfor %} </code></pre> <p>This will give you something not very pretty but with all authors and below each one, the list of their documents that fall within one of the <code>category_list</code> (OR condition, for AND, you need to filter the query for each category instead of using <code>__in</code>). <br /></p> <p>If the author has no document in the <code>category_list</code>, it wil be listed without documents below him.</p> <p><br /> Something like:</p> <pre><code>aut1 tit2 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely published in 2010. If not, .something_magical_happens_to_documents didn't work. tit1 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely published in 2010. If not, .something_magical_happens_to_documents didn't work. aut2 tit3 is almost certainly in one of these categories: ['c2', 'c3'] and was absolutely published in 2010. If not, .something_magical_happens_to_documents didn't work. aut3 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload