How to get latest unique entries from sqlite db with the counter of entries via Django ORM

2024/11/14 18:56:57

I have a SQLite db which looks like this:

|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  |

I need a Django query to select this:

|1 |16 Oct  | GB | GB1   | 2 |
|3 |17 Oct  | ES | ES1   | 3 |
|6 |10 Oct  | CH | CH1   | 1 |

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?

Answer

You can use Django annotate() and value() together: link.

when a values() clause is used to constrain the columns that are returned in the result set, the method for evaluating annotations is slightly different. Instead of returning an annotated result for each result in the original QuerySet, the original results are grouped according to the unique combinations of the fields specified in the values() clause. An annotation is then provided for each unique group; the annotation is computed over all members of the group.

Your ORM query should looks like this:

queryset = Model.objects.values("Lang").annotate(max_datetime=Max("DateTime"),count=Count("ID")
).values("ID", "max_datetime", "Lang", "Details", "count"
)
https://en.xdnf.cn/q/119090.html

Related Q&A

What does this code %.8f% do in python? [duplicate]

This question already has answers here:What does % do to strings in Python? [duplicate](4 answers)Closed 6 years ago.I am editing a code line to pass the rate in quotes:OO000OO00O0O0O000 [rate]=O0O0OO…

How to append a selection of a numpy array to an empty numpy array

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are …

Error saving and loading a list of matrices

I have a list "data_list", and I would save it in order to load it in another script. First of all I converted it in an array, in this way:data_array = np.array(data_list)Then I saved it:np.s…

Trying to interact with HTML page elements, but none of them are found

Im trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly…

Duplicating an XML element and adding it to a specific position in XML file using python

I have a xml file in which content looks like this: xml_content_to_search = <Document ProviderID="TD" DecimalMarker="comma" Website="https://erc-viewer.sap.com/"> &l…

How do I fix this Gets server error, which is causing display issues?

The list in the left column of ontariocourts311.ca, along with the body of the page under the image intermittently fail to display (which is fixed by refreshing the page). Im a Noob, and have tried var…

Installing Scipy for Windows

I am trying to install Scipy on my computer. I did it by using the command pip install Scipy. (pip & numpy are up-to-date and I am using Python 3.6) I also tried it with Pycharm, but it didnt worke…

Python Opencv, dont put circle on the video

I wrote the following script with OpenCVimport cv2 import numpy as npcap = cv2.VideoCapture(0) ix, iy = -1, -1def draw_circle(event, x, y, flags, param):global ixglobal iyix,iy = x,yif event == cv2.EVE…

List coordinates between a set of coordinates

This should be fairly easy, but Im getting a headache from trying to figure it out. I want to list all the coordinates between two points. Like so:1: (1,1) 2: (1,3) In between: (1,2)Or1: (1,1) 2: (5,1)…

NA values in column is not NaN Pandas Python [duplicate]

This question already has answers here:Prevent pandas from interpreting NA as NaN in a string(7 answers)Closed 2 years ago.I got a CSV File. I got a column Product. One of the products in it, called NA…