Reputation: 711
I am new in django, I want to print my data, i am using this function to get data from table, page_data = Pages.objects.all().filter(id=id)
, i am getting this response <QuerySet [<Pages: Page 567>]>
, why it shows only title column data ? It should show all the data, can anyone plese help me to show all the data whitin the table with print
in cmd ?
Upvotes: 6
Views: 10879
Reputation: 16032
The simplest solution is to use QuerySet.values()
:
pages = Page.objects.all()
print(pages.values())
# <QuerySet [{'id': 1, 'col1': 'val1'}, {'id': 2, 'col1': 'val2'}}]>
Upvotes: 3
Reputation: 1587
To get a dictionary representation of a Django model object, you can access the __dict__
attribute:
model_object = Pages.objects.all().filter(id=id).first()
print(model_object.__dict__)
As a bonus, to do a colorized pretty print (e.g. in the ./manage.py shell
REPL environment):
>>> from rich import pretty, print
>>> pretty.install()
>>> model_object.__dict__
Upvotes: 1
Reputation: 5190
Perhaps you should be looking at overriding __str__()
method to return whatever data you want from an object in Django Model.
Here you go.
https://docs.djangoproject.com/en/2.2/ref/models/instances/#str
You can also get any column value of the object using the below format.
object_name.column1_name
object_name.column2_name
Upvotes: 1
Reputation: 989
You can see each property data by accessing using the property name like
page_data.id
page_data.title
By default the the query set will display only the primary key of the record as a tag for the query set which is why you see the id value 567 in [<Pages: Page 567>]
Upvotes: 2