Naimish Gedia
Naimish Gedia

Reputation: 65

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?

Upvotes: 4

Views: 2426

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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

Upvotes: 4

Related Questions