Compare saved date field with new unsaved object - python

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'))

Related

Extracting month from date in flaskform with different format

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

how to subtract a date from today in django model

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.

Filter Django DB object with a manipulated keyword

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():

NDB query by time part of DateTimeProperty

I need to query items that where added at some time of day, ignoring which day. I only save the DateTime of when the item was added.
Comparing datetime.Time to DateTimeProperty gives an error, and DateTimeProperty does not have a time() method.
The only way would be to store the time of day as a separate property. An int will be fine, you can store it as seconds. You could do this explicitly (ie set the time property at the same time you set the datetime, or use a computedproperty to automatically set the value.
I've the same problem (more or less) and I think that the only way is build our own date class model.
Something like this:
class Date(ndb.model):
...
day = ndb.IntegerProperty()
month = ndb.IntegerProperty()
year = ndb.IntegerProperty()
...
class Anything(ndb.Model):
...
dateTime = ndb.StructuredProperty(Date)
...
Instead of:
class Anything(ndb.Model):
...
dateTime = ndb.DateTimeProperty()
...
But also I think that we should have other way to do this easier.

Django filter by datetime, after converting string to datetime

I'm trying to filter my query by a datetime. This datetime is the datetime for the value range the customer wants to know information for. I'm trying to set it to the first of the month selected by the customer. I pass the month number convert it to the correct string format and then convert to a datetime object because simply looking for the string object was returning no values and Django's documentation says you need to do it like:
pub_date__gte=datetime(2005, 1, 30)
Code:
if 'billing-report' in request.POST:
customer_id = int(post_data['selected_customer'])
This is the code I use to get the selected customer date and turn it into a tupple
if 'billing-report' in request.POST:
customer_id = int(post_data['selected_customer'])
selected_date = int(post_data['month'])
if selected_date < 10:
selected_date = '0'+str(selected_date)
year = datetime.now()
year = year.year
query_date = str(year) + '-' + str(selected_date) + '-01'
query_date_filter = datetime.strptime(query_date, "%Y-%m-%d")
compute_usages = ComputeUsages.objects.filter(customer_id = customer_id).filter(values_date = query_date_filter)
django debug shows: datetime.datetime(2014, 10, 1, 0, 0)
query_date looks like: 2014-07-01 before it is converted
.
No error but no data is returned
I used to use:
compute_usages = ComputeUsages.objects.filter(customer_id = customer_id).filter(values_date = datetime(query_date_filter))
which was causing the error. I'm sorry for changing my question as it evolved that is why I'm re-including what I was doing before so the comments make sense.
Almost all of that code is irrelevant to your question.
I don't understand why you are calling datetime on query_date. That is already a datetime, as you know because you converted it to one with strptime earlier. So there's no need for any more conversion:
ComputeUsages.objects.filter(customer_id=customer_id).filter(values_date=query_date)
Well after spending sometime exploring setting the query filter to datetime(year, month, day) I came to the realization that django doesn't convert it to a neutral datetime format it has to match exactly. Also my data in the database had the year, day, month.
Learning point:
You have to use the datetime() exactly how it is in the database django does not convert to a neutral format and compare. I assumed it was like writing a query and saying to_date or to_timestamp where the db will take your format and convert it to a neutral format to compare against the rest of the db.
Here is the correct way
compute_usages = ComputeUsages.objects.filter(customer_id = customer_id).filter(values_date = datetime(year, day, selected_month))

Categories

Resources