4

I'm making a query to get the number of days from the date that a person bought a new phone until today.

device_record = Devc.objects.annotate(duration = todays_date - F('datebuy'))\
    .order_by('datebuy')

When I retrieve the data, i got this 3150 days, 20:08:00 .

How can I do to remove the time because I just want to display the number of days?

I've tried:

device_record = Devc.objects.annotate(duration = (todays_date - F('datebuy')).days)\
        .order_by('datebuy')

the error returned: 'F' object has no attribute 'days'

I define todays_date like this todays_date = datetime.now().date(), the datebuy is DateTimeField

Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • What is the type of todays_date? Maybe this is helpful [subtrack two dates](https://stackoverflow.com/questions/2861770/how-do-i-subtract-two-dates-in-django-python?rq=1) – Daedalus Dec 26 '18 at 09:05
  • I define todays_date like this `todays_date = datetime.now().date()`, the `datebuy` is `DateTimeField` –  Dec 26 '18 at 09:08
  • @Samantha Did you ever find a solution to this? – rmcsharry Apr 17 '20 at 22:03

4 Answers4

1

You could try to subtract the timestamps and then retrieve the days.

from datetime import datetime, timedelta

now = datetime.now()
yesterday =  now - timedelta(days=1)
duration = now - yesterday

print duration.days

Output:

1

Daedalus
  • 295
  • 2
  • 17
1
device_record = Devc.objects.annotate(duration = todays_date - F('datebuy')).order_by('datebuy')

device_record = device_record.annotate(duration_in_days = F('duration__day'))
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 5
    While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. – David Buck May 04 '20 at 07:44
0

If you need just a number of days a user buy a phone. You can query like this device_record = Devc.objects.order_by('datebuy')

Then in template {{ your_date_field|timesince:current_date_taht_can_be_return_from_view }}

shafik
  • 6,098
  • 5
  • 32
  • 50
0

from django.db.models.functions import Cast, ExtractDay, TruncDate

device_record = Devc.objects.annotate(
    duration = Cast(
        ExtractDay(
            TruncDate(todays_date) - TruncDate(F('datebuy'))
        ),
        IntegerField()
    ).order_by('datebuy')
Manuel Fedele
  • 1,390
  • 13
  • 25
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 17:08