0

I have due_date column in DB and trying to filter rows by the value of that column. So the values in the column looks like "2020-01-20" and I am trying to filter it till year and month. NOT till Day In the function below:

def retrieve(request):

    date = "2020-01"       

    tasks = Task_Histories.objects.all().filter(due_date=date)
    serializer = InvocesSerializer(invoces, many=True)
    return Response(serializer.data)

How can I arrange DB column value before matching it in the filter()?

2 Answers 2

2

Use startswith so you can filter by month or by year, or millennium :)

Task_Histories.objects.filter(due_date__startswith=date)
Sign up to request clarification or add additional context in comments.

1 Comment

this little touch saved me a lot. Thank you. will accept it.
1

Assuming you are using the Django DateField in your models. If so, use __year and __month filter

tasks = Task_Histories.objects.filter(due_date__year='2020',due_date__month='01')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.