Created
October 13, 2018 18:07
-
-
Save vubon/c32f1c9c68873f3183b8183446832e23 to your computer and use it in GitHub Desktop.
Python Date time object creation and filter data with Django ORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
python manage.py runscript <script name> --script-args <args> | |
""" | |
import datetime | |
from contact.v2.models import TestModel | |
from django.utils import timezone | |
def run(): | |
start_date = (timezone.now().date() - timezone.timedelta(days=1)).strftime('%Y-%m-%d') + 'T20:00:00' | |
end_date = timezone.now().date().strftime('%Y-%m-%d') + 'T20:00:00' | |
print(end_date) | |
data = TestModel.objects.filter(created_at__range=(start_date, end_date)).values() | |
print(data) | |
for date in range(12, 31): | |
start = '2018-10-{}T20:00:00'.format(date) | |
datetime_obj = datetime.datetime.strptime(start, '%Y-%m-%dT%H:%M:%S') | |
first_date = (datetime_obj - timezone.timedelta(days=1)).strftime('%Y-%m-%d') + 'T20:00:00' | |
if date == 15: | |
data = TestModel.objects.filter(created_at=start) | |
print(data) | |
# then push data into another server | |
else: | |
data = TestModel.objects.filter(created_at__range=(first_date, start)) | |
print(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment