KHAN
KHAN

Reputation: 59

Django Multiple filter with range look-up

my model:

class Attendance(models.Model):
    date = models.DateField()
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    student = models.ForeignKey(Student,  on_delete=models.CASCADE)
    attendance = models.BooleanField()

the query i am trying

att = Attendance.objects.filter(date__range=(st_date,ls_date)).filter(student__range=(1,10))

it is giving me an error :

   File "C:\Users\user1\Desktop\backend\environment\lib\site-packages\django\db\models\sql\query.py", line 1184, in build_lookup
    raise FieldError('Related Field got invalid lookup: {}'.format(lookup_name))
django.core.exceptions.FieldError: Related Field got invalid lookup: range

Upvotes: 1

Views: 1138

Answers (2)

Hady
Hady

Reputation: 21

If you want to get only 10 of item from query u can use the index

att = Attendance.objects.filter(date__range=(st_date,ls_date))[0:9]

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

It makes no sense to filter with a student__range, since there are no ranges of Students : there is no inherent order, or a successor/predecessor.

You can for example constraint the primary key of the student, or some other numerical value, with:

Attendance.objects.filter(
    date__range=(st_date,ls_date),
    student__pk__range=(1,10)
)

Upvotes: 1

Related Questions