0

I have a list of Rest objects. It's django model

class Rest(models.Model):
   product = models.ForeignKey('Product', models.DO_NOTHING)
   date = models.DateTimeField()
   rest = models.FloatField()

I want to select objects from it for today's date. I do it like this. Maybe there is some more convenient and compact way?

    for rest in rests_list:
      if rest.date.day == datetime.now().day:
         result.append(rest)

3 Answers 3

2

First - datetime.now().day will get you the day of the month (e.g. 18 if today is 18th March 2020), but you've said you want today's date. The below is on the assumption you want today's date, not the day of the month.

(NOTE: weAreStarDust pointed out that the field is a DateTimeField, not a DateField, which I missed and have updated the answer for)

The way you are doing it right now seems like it might be fetching all of the Rests from the database and then filter them in your application code (assuming rests_listcomes from something likeRest.objects.all()`. Generally, you want to do as much filtering as possible on the database query itself, and as little filtering as possible in the client code.

In this case, what you probably want to do is:

from datetime import date
Rest.objects.filter(date__date=date.today())

That will bring back only the records that have a date of today, which are the ones you want.

If you already have all the rests somehow, and you just want to filter to the ones from today then you can use:

filter(lambda x: x.date.date() == date.today(), rests_list)

That will return a filter object containing only the items in rests_list that have date == date.today(). If you want it as a list, rather than an iterable, you can do:

list(filter(lambda x: x.date.date() == date.today(), rests_list))

or for a tuple:

tuple(filter(lambda x: x.date.date() == date.today(), rests_list))

NOTE: If you actually want to be storing only a date, I would suggest considering use of a DateField (although not if you want to store any timezone information). If you want to store a DateTime, I would consider renaming the field from date to datetime, or started_at - calling the field date but having a datetime can be a bit confusing and lead to errors.

Sign up to request clarification or add additional context in comments.

Comments

0

As docs says

For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.

from datetime import datetime
Rest.objects.filter(date__date = datetime.now().day)

Comments

0

You can use the django filter for filtering and get only today's date data from your model. No need to fetch all data first and then apply loop for get today's date data. You have to write your query like ...

import datetime
Rest.objects.filter(date__date = datetime.date.today())

But be sure that timezone should be same for database server and web server

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.