I have the following table:
class OverallAdvise(mixins.OrdMixin, mixins.EqMixin, models.Model):
section = models.ForeignKey('quest.Section',
on_delete=models.CASCADE,
related_name='section_owner')
range_start = models.IntegerField(blank=True, null=True)
range_end = models.IntegerField(blank=True, null=True)
get_advise = models.CharField(max_length=500)
Then, in serializer I'm trying to select the get_advise
based on a calculated score.
My query is:
get_overall_advise = OverallAdvise.objects.filter(section_id = section_id, range_start__gte = section_overall_score, range_end__lte = section_overall_score).values("get_advise")
but it's not working.
When I use only section_id
in my query, it's working.
section_id=6
and section_overall_score=8
are given.
I need something like range_start<=6<=range_end
.
This is an instance of my mysql
table:
Can you help me please?
In your mysql
example, I see that range_start is never greater than range_end, so range_start__gte=value
and range_end__lte=value
won't match any entry since value is the same. Except maybe range_start == range_end == value
So if you want range_start <= 6 <= range_end
you should use range_start__lte=6, range_end__gte=6
When writing you ORM query as if statements you will see the error
for row in OverallAdvise.objects.all():
if row.section_id == section_id:
if range_start >= section_overall_score:
if range_end <= section_overall_score:
return row.get_advice
This will never be successful as range_start
<= range_end
. I think you wanted to switch lte
and gte
around in your query.
get_overall_advise = OverallAdvise.objects.filter(section_id = section_id, range_start__lte = section_overall_score, range_end__gte = section_overall_score).values("get_advise")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.