5๐
โ
I was able to get this to work using the following:
blog_ids = Blog.objects.filter(is_published = True)\
.order_by('author_id', '-published_date')\
.distinct('author_id')\
.values('id', flat=True)
blog_list = Blog.objects.filter(id__in=blog_ids)\
.order_by('-published_date')
Which gets exactly what I wanted!!!!
More detail on this method can be found here: https://stackoverflow.com/a/32760239/2990550
๐คMegawatt
1๐
You can read this post in official Djang documentation.
Based on this post, when you specify field names in distinct clause, you must provide an order_by() in the QuerySet, and the fields in order_by() must start with the fields in distinct(), in the same order. To get the goal you said above, following this :
from django.db.models import Max
blog_list = Blog.objects.filter(is_published=True).values('author').annotate(Max('published_date'))
- [Django]-MS Access backend for django
- [Django]-Can I do Django & Python web development using Xcode 3.2?
- [Django]-Django extra javascript is not working in the child page
- [Django]-Dyld: Library not loaded: @rpath/libpcre.1.dylib
- [Django]-Is it a bad practice to use sleep() in a web server in production?
0๐
Try this solution
blog_list = Blog.objects.filter(is_published=True).distinct('author').order_by('-published_date', 'author')
- [Django]-Login and registration form on one page with Django
- [Django]-Django, a problem about zip file response
- [Django]-How does one create a custom 403 page in Django?
- [Django]-How to allow editing of templates for email by admin user in Django?
- [Django]-Django Channels 2.0: call Consumer's channel by its name
0๐
I think you should try this:
Blog.objects.filter(is_published=True).order_by('-published_date').order_by('author').distinct('author')
It will give what you want.
here you will get all authors latest blogs.
๐คAshish
- [Django]-How to get django form wizard extra_context to show on template?
- [Django]-Django ORM: Joining QuerySets
- [Django]-How do I pass custom variable values to the django admin interface?
0๐
Try this code
Now this is remove null values
from django.db.models import Max
Blog.objects.filter(pk__in=Blog.objects.order_by('-published_date').values(
'author_id').annotate(max_id=Max('pk')).values('max_id'),
is_published=True, author_id__isnull=False).order_by('-author_id')
๐คGorkhali Khadka
Source:stackexchange.com