Django sort product after first three records


There are two fields on my page:
1-Featured listings
2-All listings

I want to post only three ads in featured listings
I want to list all advertisements in all advertisements
However, I do not want to show the ads I have posted in the featured ads in all ads.

My sample code is as follows

activitiy = Activity.objects.filter(etkinlik_durum = True).all().order_by('-id')[:3]
activitiylist = Activity.objects.filter(etkinlik_durum = True).all().order_by('id')

I’m sorry for my bad english. I hope I have conveyed my problem to you correctly.

First, if you use filter, you don’t also need to use all. You can remove both instances of all in your queries and not change anything.

For not including the first list in the second, you’ve got at least two options:

  • You can get a list of the id in your activity queryset and use the exclude method to remove the entries of activity from activitylist.
    activity_ids = activity.values_list('id', flat=True)
    activitylist = Activity.objects.filter(...).exclude(id__in=activity_ids).order_by('id')


  • You could use the difference method on the queryset directly
    activitylist = Activity.objects.filter(...).difference(activity).order_by('id')
1 Like

My problem is solved. Thank you for return