How to get latest unique entries from sqlite db with the counter of entries via Django ORM
<p>I have a SQLite db which looks like this:</p>

<pre><code>|ID|DateTime|Lang|Details|
|1 |16 Oct  | GB |  GB1  |
|2 |15 Oct  | GB |  GB2  |
|3 |17 Oct  | ES |  ES1  |
|4 |13 Oct  | ES |  ES2  |
|5 |15 Oct  | ES |  ES3  |
|6 |10 Oct  | CH |  CH1  |
</code></pre>

<p>I need a Django query to select this:</p>

<pre><code>|1 |16 Oct  | GB | GB1   | 2 |
|3 |17 Oct  | ES | ES1   | 3 |
|6 |10 Oct  | CH | CH1   | 1 |
</code></pre>

<p>So this is unique (by Lang) latest (by DateTime) entries with the number of occurrences (by Lang). Is it possible to do this with a single SQL or Django-ORM query?</p>

<p>As you want distinct entries by "Lang" and latest entries by "DateTime", below query will help you,</p>

<p>queryset = Model.objects.distinct("Lang").order_by("-DateTime")</p>

python django sqlite django-orm