Doing Groupby in Django ORMs
<p>I have a simple table with the following values </p>

<pre><code> "uid": "some_uid",
 "name": "test1",
 "trig_time": 1234, #unix timestamp
 "notify_time": 1235  #unix timestamp
</code></pre>

<p>I have lot of tuples which may or may not have the same name. I intend to groupby results based on name and have the no of counts and the latest trig_time returned as result. For example ,for the following tuples </p>

<pre><code>name,trig_time,notify_time
test1,1234,1235
test1,1236,1237
test2,1238,1239
</code></pre>

<p>expected result</p>

<pre><code>name,count,latest_trig_time
test1,2,1236
test2,1,1238
</code></pre>

<p>I tried with the following query. I am not sure, what I am doing wrong.</p>

<pre><code>queryset = Trig.objects.filter(uid=uid).annotate(count=Count('name')).order_by('name')
</code></pre>

<p>Based on the comments,
I am using Djangorestframework the following is my model</p>

<pre><code>class Trig(models.Model):
    uid = models.CharField(max_length=100, blank=False)
    name = models.CharField(max_length=100, blank=False)
    trig_time = models.BigIntegerField(blank=False)
    notify_time = models.BigIntegerField(default=0)

    class Meta:
        unique_together = ('name', 'uid', 'trig_time')
</code></pre>

<p>do I have to change my serializer code as well ? I executed the query in django shell and it worked fine. But in the server, I get the following error </p>

<blockquote>
  <p>The serializer field might be named incorrectly and not match any
  attribute or key on the <code>dict</code> instance.</p>
</blockquote>

<p>The following is my serializer code </p>

<pre><code>class TrigSerializer(serializers.ModelSerializer):
    class Meta:
        model = Trig
        fields = ('uid','name','trig_time','notify_time') 
</code></pre>