Django queryset filter problem


I need to write a queryset which filters 10 higher values and 10 lower values than a given value.

class Toy(models.Model):
    name = models.CharField()
    price = models.IntegerField()

With given price 56, I want to get a list of 10 toys whose prices are higher than the given value and most close to the given value and the same for the lower values.
Finally, I want to include the toy with given price as well.

I am not sure how I can filter this out.

Thanks in advance.


Take a look at the 2 last examples of the cheatsheet with annotate, it might give you an idea.

Feel free to post any doubt.

1 Like

Thanks @marcorichetta

I think I actually did it as same as in the example. :smiley:

1 Like

Hello!! Friends ! I am creating an E-learning Site where I have to create course for teacher …So I successfully Created Course with create view and List view Now …I want to create Sections within the Course Like when teacher hit course title fo to course detail page its work fine…I have also created course detail page succesfully…Now the problem is when User/teacher create subject with Course Selected as a Foreign Key it will display creatd subject in subject list view …but when I choose other course with foreign key .its also showing in List view…I want only display selected course(ForignKey) subject in ListView…Anypne can help?I will also put Screen shot of my problem