I have table in Oracle, which has field of DATE type. Also I have model in Django with DateTimeField. I want to save datetime in Oracle's DATE field, but Django ORM raises such exception:
DatabaseError: ORA-01830: date format picture ends before converting entire input string
I tried to use Django DateField, but it didn't save datetime, only date. How can I save datetime in Oracle using Django (I don't want to use DATETIME field in Oracle because of legacy problems).
My model:
class MyModel(models.Model):
filled_date = models.DateTimeField(db_column='filled_date')
I found the solution for problem.
Oracle waits format 'YYYY-MM-DD HH24:MI:SS', but datetime.datetime.now() returns string like this: u'2013-10-18 05:50:44.332577'.
The solution:
model.filled_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
model.save()
Related
I want to store time as unix timestamp in my MYSQL database, I have django project with model:
date = models.DateField()
But I didn't find any models.Timestamp()
or anything similiar. Is there a way to create timestamp column for MYSQL Db in Django? I found some articles here on stack but they are 5+ years old so there might a be a better solution now.
In Django, one usually uses a DateTimeField [Django-doc] for that. It is a column that thus stores a combination of date and time.
One can let Django automatically intialize (or update) the timestamp if the record is constructed or updated with auto_now_add=True [Django-doc] to initialize it when the record was created, and auto_now=True [Django-doc] to update. So it is a common pattern to see a (base)model like:
class TimestampModel(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
such that subclasses of the TimestampModel thus have two extra columns created and updated that store the time when the object was created and last updated respectively.
A datetime column has a larger range, as is specified in the MySQL documentation:
The DATETIME type is used for values that contain both date and time
parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD hh:mm:ss' format. The supported range is '1000-01-01 00:00:00' to
'9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date
and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
I have built a web app in Python and Flask and am having trouble pulling the date and time from my SQLite database.
I enter the date into the DB with the following line-
order.order_placed = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Which with my current example enters the following into the DB -
2018-05-01 12:08:49
But when I call order.order_placed I get datetime.date(2018, 5, 1)
Even if I call str(order.order_placed) I get '2018-05-01'
Can someone help me get the full date and time out of the database? Thanks!
It's possible that you're using DateField when in actuality you want to use DateTimeField.
Furthermore, you don't need to call strftime before storing the data. Peewee works nicely with Python datetime objects.
I've Django model which has foreign keys associated with other models. Each model is having same field names(attributes) created_at and updated_at
In every django queryset results I'll be getting datetime values.
Model.objects.all().values('created_at')
But I want to format the datetime field to "DD-MM-YYYY HH:MM:SS" and trim down the milliseconds in the django query results.
If I use "extra" and and date_trunc_sql like the following command
dt = connection.ops.date_trunc_sql('day','created_date')
objects.extra({'date':dt}).values('date')
Which works fine. But If I query like the following, its raising ambiguous statement error.
objects.extra({'date':dt}).values('date', 'x', 'y', 'z')
How to overcome this problem?
Solved it via #Yannics answer at: https://stackoverflow.com/a/60924664/5804947
This also avoids using extra which should be "a last resort" due to Django docs.
from django.db.models import F, Func, Value, CharField
qs.annotate(
formatted_date=Func(
F('created_at'),
Value('DD-MM-YYYY HH:MM:SS'),
function='to_char',
output_field=CharField()
)
)
Got the solution.
data = list(Model.objects.extra(select={'date':"to_char(<DATABASENAME>_<TableName>.created_at, 'YYYY-MM-DD hh:mi AM')"}).values_list('date', flat='true')
It's not just tablename.attribute, it should be dbname_tablename.attribute when we have multiple databases(ambiguous)
which will result list of created_at datetime values trimmed to 'YYYY-MM-DD HH:MM' format.
I don't think values() function would anything related to formatting datetime result. But why does that bother you? Can't you convert them to proper format when you try to display them? If you try to render them in the template, django has template filter date for formatting your datetime value: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#date
Currently in our database the date field is entered as a string (ex: 11/7/2009). Is there anyway in my models.py file to convert this field from say a TextField to DateField so that in the Admin Console a user could select the date from the calendar view instead of having to enter it in manually? Perhaps parse it on the fly?
date = models.TextField()
convert to..
date = models.DateField()
Your best bet is to start storing the data correctly. In order to do this you'll want to alter the table to add a new field (in the database) with the date type. Parse the existing date data from the charfield into this new field and then finally alter the table to drop the charfield and rename the the temporary date field appropriately
This process could be simplified by using django-south to manage the migrations. You'll need 3 migrations: schemamigration to add the temporary field, datamigration to convert the data, schemamigration to drop the old column and rename the new one.
You could try using a custom form in the admin...you might be able to force the conversion at runtime, but it's really not a great idea since you really should be storing data correctly. I've used custom forms before in the admin, but not for this so I can't be sure if it would work.
After changing the column in MSSQL to type date we still were having the same issue. I should have also mentioned we were using pyodbc to help server our backend. The change we made was within the pyodbc code in the operation.py file. A type-except was added.
def convert_values(self, value, field):
.......
elif field and field.get_internal_type() == 'DateField':
try:
value = value.date() # extract date
#ADDED THE FOLLOWING TO CATCH THE ERROR
except AttributeError:
value = datetime.datetime.strptime(value, '%Y-%m-%d').date()
After we added this the django admin console was displaying the Calendar widget.
I'm having a bit of trouble with django's DateField model field. Shouldn't it be able to accept fiveDaysLater as a valid date object? When I try to add fiveDaysLater into the database, I get an error saying cannot add null value to date. However, the second I change the date field to a regular CharField, the fiveDaysLater value is added to the database with no problem. fyi if I print fiveDaysLater, I get 2011-09-28
My view:
def myView():
now = datetime.date.today()
fiveDaysLater = now + datetime.timedelta(days=5)
newDate = Speech(date = fiveDaysLater)
newDate.save()
My model
class Speech(models.Model):
date = models.DateField()
"However, the second I change the date field to a regular CharField..." Just a suspicion but if you made this change in your code, make sure to delete and recreated the Speech table using syncdb, otherwise, sqlite will not be aware of this change. (or you could change the datatype using sqlite exporer for firefox or something like that...)