Home > Software engineering >  Django: how to filter posts based on view in django
Django: how to filter posts based on view in django

Time:05-06

i want to filter posts based on views e.g view>100 that is to filter course if only the view is greater is 100 views but it keeps showing this error SyntaxError: positional argument follows keyword argument. the way i am filtering the posts is the issue but i don't know the right way to do this. I have a field views = models.In... in my models.py, so that is why i am trying to filter course like course = Course.objects.filter(views>100) then it shows the error

models.py

class Course(models.Model):
    course_title = models.CharField(max_length=100, null=True, blank=True)
    slug = models.SlugField(unique=True)
    views = models.IntegerField(default=0)
    

views.py

def index(request):
    pop_courses = Course.objects.filter(course_publish_status="published", views>100).order_by('?')
    

CodePudding user response:

You need to use the __gt lookup to perform this filter

Course.objects.filter(course_publish_status="published", views__gt=100).order_by('?')

CodePudding user response:

You can filter with the __gt lookup [Django-doc]:

Course.objects.filter(
    course_publish_status='published',
    views__gt=100
).order_by('?')
  • Related