4

i am selecting last object from my table using following Code

data= MyModel.objects.last()

This is Displaying object. i want to display full array of 'data' object.for that i am using Following Code

data= MyModel.objects.last().values()

However its not Working.How to do that?

  • If you have a single object without a queryset: https://stackoverflow.com/q/21925671 – djvg Mar 21 '23 at 12:38

1 Answers1

4

You should swap .values(…) [Django-doc] and .last() [Django-doc], since before the .last(), you are working with a Manager or QuerySet, after .last() you are working with a MyModel object:

data= MyModel.objects.values().last()

That being said, using .values() is often not a good idea. It erodes the logic layer a model provides. Only in specific cases, like a GROUP BY on a certain value it is a good idea to use .values().

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555