6๐
{{top_post}}
is a query set (slicing a queryset also produces a queryset), so it should display that. When you loop over the queryset, as you do over latest
, you display the objects inside.
{{top_post.0}}
would give you the object top_post
contains.
Alternatively you can use
top_post = Post.objects.filter(category='1').first()
to get the object directly.
1๐
You should implement the __str__
method in the Post model where you define what to print.
Moreover you should .get
instead of .filter
if you expect one object to be returned.
- [Django]-Django 1.9: Should I avoid importing models during `django.setup()`?
- [Django]-Python & Django on a Mac: Illegal hardware instruction
- [Django]-Empty label in widget Select
- [Django]-Many to many relation. ORM Django
- [Django]-Why am getting a MultipleObjectsReturned error inside a try block?
- [Django]-Easiest way to write a Python program with access to Django database functionality
- [Django]-Using Django-Q in production
0๐
If you are using filter then I am assuming you want more than one value else use the get method.
You can convert the queryset into a list and then play around the list.
top_post = list(Post.objects.filter(category='1')[:1])[0]
Now top_post
will give the first object in the list.
0๐
Just to pile on to last bit about getting the actual value out of the QuerySet objects. This is what I do.
list_of_things = TableName.objects.all().values_list('thing', flat=True)
values_list
lets me select what column(s) to return and flat=True
extracts (for lack of a better term) each column as an object, thus loading up the list.