Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling related models in Django for use in Django-Piston
    text
    copied!<p>I have setup like so (changed for simplicity)</p> <pre><code>class Author(models.Model) name = models.CharField(max_length=100) ... class Document(models.Model): title = models.CharField(max_length=200) content - models.TextField() author = models.ForeignKey("Author", related_name="documents") date_published = models.DateTimeField() categories = models.ManyToManyField("Category") class Category(models.Model): name = models.CharField(max_length=100) </code></pre> <p>I'm pulling in the Author records but I only want to pull in related document records for each author that match specific criteria -- say, date_published and category.</p> <p>I know the easy way to do this would be to pull in the records as a list of dictionaries using <code>Author.objects.values()</code>, looping through each record and running:</p> <pre><code>author['documents']=Document.objects.filter(categories__in=[category_list], date_published__year=year)` </code></pre> <p>However, this is being generated for django-piston, and it seems to be happiest (particularly if you're defining your own fields!) if you return a QuerySet object.</p> <p>Part of this may be because I made changes to the base django-piston code. Basically, the <a href="http://bitbucket.org/jespern/django-piston/" rel="nofollow noreferrer">current version of the code</a> here overwrites the <code>fields</code> value. I changed this code so that I could change the <code>fields</code> value for a Handler based on the request (so I could provide more details if the request was for a specific resource).</p> <p>So I guess my question is three-fold:</p> <ol> <li>Is there a way to filter or somehow limit the subrecords of a record (i.e. filter <code>documents</code> for each <code>author.documents</code>)</li> <li>If not, what is a functional way of doing this that also works with django-piston?</li> <li>Is there some easier, better way to do what I'm trying to do (display all the authors without their documents if an id is not given, but displaying the sub-records if filtering to just one author)?</li> </ol> <h1>Clarification</h1> <p>Okay, just to be clear, here is the pseudocode that I want:</p> <pre><code>def perhaps_impossible_view(request, categories=None, year=None): authors = Author.objects.all() authors.something_magical_happens_to_documents(category__in=[categories], date_published__year=year) return render_to_response('blar.html', locals(), RequestContext(request)) </code></pre> <p>So that if I <strong>were</strong> to put it in a template, this would work without any modifications:</p> <pre><code>{% for a in authors %} {% for d in authors.documents.all %} {{ 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. {% endfor %} {% endfor %} </code></pre> <p><code>something_magical_happens_to_documents</code> has to run and actually change the contents of <code>documents</code> for each author record. It seems like this should be possible, but perhaps it isn't?</p>
 

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