'how can I get month from date in django models
I want to make a query and return only the month of the year of the date field from my models then compare the return result to the current month of the year.
current_date = datetime.date.today()
_history = PayrollModel.objects.filter(employee_id_id=employee_id)
if(_history == None or _history.count() == 0):
return True
if(_history != None and _history.count() != 0):
# entity = _history.get(month_year=current_date)
for history in _history:
print(history.employee_id_id)
if(history.month_year__month != current_date.month and history.month_year.year == current_date.year):
return True
else:
return False
Solution 1:[1]
Filtering by month is a built-in queryset feature. You don't show your History model, so I am writing as if the relevant date stamp field is called xdate (date might read confusingly)
So you need to further filter:
today = datetime.date.today()
current_year = today.year
current_month = today.month
_history = _history.filter( xdate__year = current_year, xdate__month = current_month)
for history in _history:
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nigel222 |
