6

Filtering the Django model to get the last index

 3 years ago
source link: https://www.codesd.com/item/filtering-the-django-model-to-get-the-last-index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Filtering the Django model to get the last index

advertisements

Is it possible to filter Django model to get the last index.

Here is my view

order = Order.objects.reverse("order_number")[-1]

But it gets the first index.


The subscript [-1] selects the last item in a list or tuple and, given that the query set was reversed, this would be the first item, not the last. You could try:

order = Order.objects.reverse("order_number")[0]

but you can't pass a sort key to reverse() so this code would fail. If you have a default sorting order set up for the table you can just do this:

order = Order.objects.last()

It might be better to use an explicit order_by():

order = Order.objects.order_by("order_number").last()

Related Articles

How to get the last indexed index to Solr?

I want to know how to get/search last indexed record in Apache Solr..? When the existing record is updated then it goes to end of all the records...so I want to get that last indexed record. thanks..You could add a 'timestamp' field to your Solr sche

How to get the last index in a JSON matrix when you do not know how big the array is going to be?

I have an array of JSON objects. This array is within a for loop and it keeps adding them to the array as the information becomes available from the database. The number of the objects in the array might not be the same as the number of results retur

How to get the last index of a list?

Suppose I've the following list: list1 = [1, 2, 33, 51] ^ | indices 0 1 2 3 How do I obtain the last index, which in this case would be 3, of that list?len(list1)-1 is definitely the way to go, but if you absolutely need a list that has a function th

Increase the size of the table and insert the item at the last index

i have dynamic array size. i want to increase the size by 1, and insert a new element at the last index array. how to do this? for example now: my array size is 2, when i do looping, i increase the array size by 1 and try to insert a new element at t

String.split causing a line break on the last index

When I split my string I get a linebreak after the last index, does anyone know what is causing this? Code: String string = "1234324:Kalle" public void splitString(String string){ if (string.contains(":")) { String[] parts = string.spl

data.frame: searches the last index of a value in each row

I have a data.frame like a b c d 1 1 0 0 1 2 1 1 0 0 3 0 1 0 0 4 1 0 1 0 5 1 0 0 0 Which I generated using df<- data.frame(a=sample(0:1,5,replace=T),b=sample(0:1,5,replace=T),c=sample(0:1,5,replace=T),d=sample(0:1,5,replace=T)) How can I get the resu

Find the last index in the column of a specific value / text - VBA

I currently have a sorted column that looks something like this: Cat Cat Cat Cat Dog Dog Dog Dog I want to find the last index of cat (which in this case is 4) and the last index of dog (which in this case is 8) I tried the following: Dim Last_Index_

Replace the last index of, with and in jQuery / JavaScript

i want to replace the last index of comma (,)in string with and. eg . a,b,c with 'a,b and c' eg q,w,e with q,w and eDEMO lastIndexOf finds the last index of the parameter string passed in it. var x = 'a,b,c'; var pos = x.lastIndexOf(','); x = x.subst

How do I check if the current index is the last index in the table?

How do I say? for (i = 0; i < arraySize; i++) { if (someArray[i + 1] !NULL) { //do this } else { //do something else } } In other words, I want to check if the current index is the last index in the array. The current code is my best guess but it isn

Find the last index of the first match in Java

Lets say I have this string String s ="stackjomvammssastackvmlmvlrstack" And I want to find the last index of the first match of the substring "stack" which is (index=)4 in my example. How will I do that? Here's what I have done so far

How to make iterate from the last index to the first in the array

bestSword = { {name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0}, {name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0}, {name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0}, {name = 'sss' , lvl = 55,

Minimum number of steps required to reach the last index

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

Python means to delete all values ​​after the last index of & ldquo; x & rdquo; in the Python list

I have searched for a similar topic, as I assume someone must have come across this problem before. Any links to relevant topics are appreciated :) I want to delete all values past (and including) the last index of "x". Given the list: a = [0, 1

* ngFor: the last index of the loop is null

This question already has an answer here: *ngIf and *ngFor on same element causing error 9 answers This is my code in a minimal form: <div> {{ 'This is all comments Array =>'}}<br> {{selectedPost.comments}} <div *ngIf="!commentsLoad

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK