Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Keep in mind that a QuerySet is more than just a simple database query, so the "expected result" should be that it contains additional information. One simple way to do what you want (and I'd say it's a Django Way) is to define a <a href="https://docs.djangoproject.com/en/dev/topics/db/managers/" rel="nofollow">model manager</a> that returns only what you want. Something like:</p> <pre><code>import simplejson class StreamManager(models.Manager): def all_serialized(self): qset = super(StreamManager, self).get_query_set() return simplejson.dumps( [{ 'id':item.id, 'name':item.name, 'network':item.network.name, 'score':item.score, 'custom_url':item.custom_url,} for item in qset]) </code></pre> <p>then put <code>objects = StreamManger()</code> in your Stream class, and you can access the stream as Stream.objects.all_serialized()</p> <p>Another way to do it would be to subclass QuerySet so it returns only the information you want and then use the serialize() method on the queryset, but I've never gotten into that and assume it would require some knowledge of how QuerySets work under the hood (it might be slightly more efficient than my solution, but since the point is to serialize everything and you want to do a foreign key lookup on each entry to find the network name, I assume you not working with a huge amount of data).</p> <p>EDIT: regarding your comment on Rob's solution, see the <a href="https://docs.djangoproject.com/en/dev/topics/serialization/" rel="nofollow">documentation</a> for serialize() - you can define the fields you want to be serialized, but I don't think it supports foreign key lookups, and you'll still get it in the QuerySet format (pk, model, and fields).</p>
    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.
 

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