Search code examples
djangodjango-querysetdjango-filter

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

Solution

  • 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)
    )