Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print all values for Single object in Django?

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?

like image 612
Naimish Gedia Avatar asked Oct 23 '25 18:10

Naimish Gedia


1 Answers

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().

like image 172
Willem Van Onsem Avatar answered Oct 26 '25 09:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!