[Fixed]-Get value from model

1👍

Try something like this:

    someModelobject = someModel.objects.order_by('creation_date')
    objList = []
    for data in someModelobject:
        objList.append(data.id)

Thanks.

0👍

This shouldn’t be a problem, you can try to use max value from query set of obj_id to generate range for querying and then get latest date from each query:

from django.core.exceptions import ObjectDoesNotExist
    result=[]
biggestID=someModel.objects.latest('obj_id') 
#someModel.objects.all().aggregate(Max('obj_id'))

for i in range(biggestID.obj_id)
   try: 
    val=someModel.objects.filter(obj_id=i).latest('creation_date')
   except ObjectDoesNotExist:
    val=None 
   if val is not None:   
     result.append(val)

You also could try:

someModel.objects.values('obj_id').annotate(creation_date=Max('creation_date'))

I want to point this out, you probably know that, but if you use your obj_id as primary key this won’t work, because primary key should be unique.

Leave a comment