models.py
import datetime as dt
class Campaign(models.Model):
enddate = models.DateField()
def rest_of_the_day(self):
now = dt.datetime.now().date()
print('printing now value')
print(now)
print(self.campaign_enddate)
return (self.campaign_enddate-now).days
in views.py
def landing_page(request):
campaigns = Campaign.objects.all().order_by('-id')
return render(request, 'core/landing_page.html',{'campaigns':campaigns})
in html template
<span>{{campaign.rest_of_the_day}}</span>
I'm trying to store an end date and show the days left to the end date in html file using rest_of_the_day function in models.py
for example : if end date is 30-01-2010 and today is 15-01-2020, i want the rest_of_the_day to show 15
however, i get a TypeError at / unsupported operand type(s) for -: 'NoneType' and 'datetime.date'
Another option is to use database functions in a query to add column to QuerySet result.
Here each campaign object in QuerySet result will have td column of type datetime.timedelta.
from django.db.models.functions import Extract, Now, Trunc
campaigns = Campaign.objects.all().order_by('-id').annotate(
td=F('enddate') - Now()
)
Or add Trunc Result td column will be datetime.timedelta object with only days
We also need to cast calculations between date and datetime to one type / output_field.
campaigns = Campaign.objects.all().order_by('-id').annotate(
td=Trunc(
F('enddate') - Now(),
'day',
output_field=models.DateField()
)
)
Note that you will be manipulating on datetime.timedelta object. Also, adding Trunc over different part (Now(), F('enddate'), whole expression, different combinations) may produce slightly different result due to how days will be rounded and substracted. You can experiment with that.
To get result as integer (days) Extract function can be added to the mix:
campaigns = Campaign.objects.all().order_by('-id').annotate(
td=Extract(
F('enddate') - Trunc(Now(), 'day', output_field=models.DateField()),
'day'
)
)
Here td will be Integer.
It seems that self.campaign_enddate is None. This makes sense, since it is not a field you defined. You can use the enddate:
class Campaign(models.Model):
enddate = models.DateField()
def rest_of_the_day(self):
return (self.enddate-dt.date.today()).days
You can however make use of the |timeuntil template filter [Django-doc] here:
<span>{{ campaign.enddate|timeuntil }}</span>
THis makes it more convenient to process the amount of time until a certain date(time) happens.
Related
I am trying to create a code made out of months and count number. The count number is easy, but I don't understand how to extract month from date form.
Model:
req_date =db.Column(db.Date, nullable=False, default=date.today())
req_code =db.Column(db.String, nullable=False, unique=True)
Form:
reqdate = DateField('Request Date')
Route:
currentyear = extract('year',form.reqdate.data)
currentmonth= extract('month', form.reqdate.data)
ronum = 'RO-'+'/'+str(currentmonth)+'/'+str(currentyear)
I tried using this function for my route I found before, but it doesn't seem to work.
What I got instead of like RO-/01/2021. I got this error:
RO-/EXTRACT(month FROM :param_1)/EXTRACT(year FROM :param_1)
Does anyone know the function to extract it? Or if the function isn't wrong, where did I go wrong?
DateField returns a datetime.date object
That means you should be able to do:
# Data from the form which has been formatted to datetime.date object
current_date = form.reqdate.data
current_year = current_date.year
current_month = current_date.month
Attributes available on datetime.date can be found here
I am trying to get a series of ages from a persons list, but the age generated change with each query because is the age in a specific event so i can accomplish this with a simple loop, extracting the timedelta from the diference:
[ (event_date - user.birth_date).days/365.25 for user in User.objects.all() ]
event_date is always a datatime.date object anr user.birth_date too. I consider it a "Static"
field or constant because it is outside the database.
This gives me the correct results, but since I am doing this many times and i have other calculations to do I wanted to generate the ages from the database using the F() expresion.
``from django.db.models import ExpressionWrapper, fields
diference = ExpressionWrapper(
event_date- F('birth_date'),
output_field=fields.DurationField())
qs_ages= self.annotate(age_dist=diference)
this should give me a field named age_dist that will be a timedelta contains the total days between the two dates, so now I should do this and should give me the same result as above.
[ user.age_dist.days/365.25 for user in User.objects.all() ]
But this does not work, the result is a time delta of microseconds
What i am doing wrong? and how should I include the static value of event_date to the expression?
And... going beyond. Is there any way to get the days of the resultant time delta from the ExpressionWrapper?
since you're doing the operation on a datetime object it'll return you a datetime object which is converted to timedelta microseconds when you use DurationField as an the output field.
you can workaround this by doing everything in the database :
age = ExpressionWrapper(
Cast((event_date.date() - F('birth_date')) / 365.25 ,output_field=fields.PositiveIntegerField()),
output_field=fields.PositiveIntegerField()
)
I take a timestamp for my Institution class:
class Institution(models.Model):
timestamp_utc = models.DateTimeField()
If there is an entry in the DB that has the same year, month and date (not time), then I want to update the value of the entry. If not, then I want to create a new entry.
The conditional is as follows:
if Institution.objects.filter(timestamp_utc.strftime("%Y/%m/%d")=b['timestamp_utc'].strftime("%Y/%m/%d")).exists():
I am getting this error:
Exception Value: keyword can't be an expression
Is there a way to filter the DB object with a manipulated keyword?
You can just filter by the date range, i.e. time stamps that are great than or equal to the date, and less that the date + 1 day.
from datetime import relativedelta
date_start = b['timestamp_utc'].strftime("%Y-%m-%d")
date_end = (b['timestamp_utc'] + relativedelta(days=1)).strftime("%Y-%m-%d")
if Institution.objects.filter(
timestamp_utc__gte=date_start, timestamp_utc__lt=date_end
).exists():
I have model with two fields:
class Event(models.Model):
date = models.DateField(_(u'Date'))
time = models.TimeField(_(u'Time'))
I need to find all objects where date&time is in 24 hours from now.
I am able to do this when using DateTime field, but I am not sure how to achieve this when fields are separated. Thanks in advance.
For the simple case (not sure if all are simple cases though...), this should do the trick:
import datetime
today = datetime.datetime.now()
tomorrow = today + datetime.timedelta(days=1)
qs_today = queryset.filter(
date=today.date(),
time__gte=today.time(),
)
qs_tomorrow = queryset.filter(
date=tomorrow.date(),
time__lt=tomorrow.time(),
)
qs = qs_today | qs_tomorrow
As you state you can do what you want with a DateTimeField, but now with the separate fields, I understand your issue is how to combine them.
Looking at the docs for DateField - your date variable is a datetime.date instance and similarly for TimeField time is a datetime.time. You can convert these into a datetime.datetime by using combine()
import datetime as dt
datetime = dt.datetime.combine(date,time)
You now have the datetime object as you would have from DateTimeField. You say in the question you can do the 24 hour from now bit from there, although let me know in comments if you need that made explicit.
Caveat I combine will fail where one of the fields is None - you state this can't happen, so I haven't added any error checking or validation of this.
EDIT
It occurs to me that the problem may not be the combination, but adding the calculated field to the Event object. You could look at this Q&A, or this. In summary you define the calculated value in a function in your class and then make it a property - either with a decorator or a function call. There's an example in the docs, adapting for your case:
def _get_datetime(self):
'''Returns a combination of date and time as a datetime'''
return dt.datetime.combine(self.date,self.time)
datetime = property(_get_datetime)
This should behave in the same way as you would expect a DateTimeField to behave.
You can use Q objects to search for "yesterday after current time or today before current time":
from django.db.models import Q
from .models import Event
from datetime import datetime, timedelta
def get_event_during_last_day():
now = datetime.now()
today = now.date()
yesterday = (now - timedelta(day=1)).date()
time = now.time()
query_yesterday = Q(date=yesterday, time__gt=time)
query_today = Q(date=today, time__lt=time)
return Event.objects.filter(query_yesterday | query_today)
I'm triying to update a record if the date is the same.
To do that, I retrieve the last record using:
saved_object = TheModel.objects.latest("date")
new_object = TheModel.objects.create(date="2014-12-15")
And then I compare the dates using:
if saved_object.date == new_object.date:
doStuffWithTheSavedObject()
saved_object.save() # update
else:
new_object.save() # insert new one
Looking at the database, I see that the dates are the same so the if statement is returning false. After debuggin it, I see that the new_object.date is a str and not a date type as I thought it should be.
My question is, how can I prepare the new_object so it has a date field with the right type without saving it into the database and without passing it a casted date manually.
Regards
Update:
class TheModel(models.Model):
date = models.DateField()
a = models.IntegerField()
b = models.IntegerField()
c = models.IntegerField()
Use datetime.date or datetime.datetime (for DateField or DateTimeField accordingly):
import datetime
new_object = TheModel(date=datetime.date(2014, 12, 15))
if you have date as string on input, use strptime
new_object = TheModel(
date=datetime.datetime.strptime('2014-12-15', '%Y-%m-%d'))