Add row number to JSON

If you’re not familiar with class inheritance and overriding methods, you might want to look at the source code for the generic Class Based Views. You’ll see lots of examples in them.
This is also a fundamental Python topic, and not specific to Django. I don’t have any references to give you, but I’d guess that there are plenty of tutorials and other examples you could learn from.
This is such a key principle of how Django is put together that I believe it would be well worth your time and effort to really become comfortable with how Python classes work.

I think I got it :slight_smile:

def prepare_results(self, qs):
    data = []
    c = 0
    for item in qs:
        if self.is_data_list:
            data.append([self.render_column(item, column) for column in self._columns])
        else:
            row = {col_data['data']: self.render_column(item, col_data['data']) for col_data in self.columns_data}
            data.append(row)
    rows_available = len(data)
    
    for item in data:
        data[c][0] = ( c+1)
        c+=1
    return data

Now I got row number 1-xxx

Almost, unfortunately it will number the rows always the same 1-xxx regardless of ascending or descending, need to detect the order now, again where and how …

Got it, any way to make it look nicer ?

    def prepare_results(self, qs):
    data = []
    c = 0
    d = 0
    for item in qs:
        if self.is_data_list:
            data.append([self.render_column(item, column) for column in self._columns])
        else:
            row = {col_data['data']: self.render_column(item, col_data['data']) for col_data in self.columns_data}
            data.append(row)
    
    if qs._query.order_by[0] == 'id':
        c = 0
    else:
        c = len(data)

    for item in data:
        if qs._query.order_by[0] == 'id':
            data[c][0] = c+1
            c+=1
        else:
            data[d][0] = c
            c -=1
            d +=1

    return data

NOP Scrap that idea, the row numbers only consisten when ordering it by the row numbers (row_id) if i sort it by time or address the row_numbers are no longer match with the previous order
id1 address1 customer1 date1
id2 address2 customer2 date2
id3 address3 customer3 date3

if ordered by other column than the id no longer match with their original record

id1 address3 customer3 date3
id3 address2 customer2 date2 *****
id2 address1 customer1 date1

Correct - because these numbers are not in any way associated with the data. They’re just arbitrary values being assigned to the results of the query. Each new query is going to generate a new set of values.