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
Related
I am trying to filter date in Django according to current date, But it's displaying mr 0 results, Please let me know Where I am mistaking.
Hers is my models.py file...
class Customer(models.Model):
name = models.CharField(null=True, blannk=True)
customer_date = models.DateTimeField(null=True, blannk=True)
here is my views.py file, where i am trying to get date according to today date...
from datetime import datetime, timedelta, time, date
def getdate(request):
today=datetime.today()
customer_data = Customer.objects.filter(customer_date=today).count()
print(customer_data, "Count Values")
I see some issue in your date filter. When you do:
datetime.datetime.today()
#2020-11-04 10:57:22.214606
this give complete timestamp.
However you want to do date match only.so, try something like code.
today = datetime.today().date()
#today=datetime.today()
customer_data = Customer.objects.filter(customer_date__date=today).count()
hope this may help your query.
I saw you mistyped blank as blannk
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.
I'm trying to get the number of months since a model is created.
My Model looks like this:
class Plan(models.Model):
title = models.CharField(max_length=50)
date_created = models.DateTimeField(default=timezone.now)
plan_type = models.IntegerField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
Now i want to make a method that returns the number of months since the date_created.
Tanks for any help :D
Comparing dates creates a datetime.timedelta object that you can use to get the difference between dates.
from datetime import timedelta
from django.utils.timezone import now
delta: timedelta = now() - plan.date_created
delta.days # Number of days between dates.
You can then use that value to convert it to months or years.
The other alternative would be a bit more complicated, but since DateTimeField returns a datetime.datetime object, you can also access the month number of the original date and compare it against todays date.
e.g.
from django.utils.timezone import now
month_diff = now().month - plan.date_created.month
The problem with the second alternative is that you then have to take into account if they are the same year or not, and if they are not you then have to take that into account when you get the month difference.
You can write a property in your model like
from django.utils import timezone
class Plan(models.Model):
title = models.CharField(max_length=50)
...
#property
def get_month(self):
return self.date_created.month - timezone.now().month
Then you can get the value like this
>>> Plan.objects.first().get_month
4
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'))
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))