Reputation: 43
I am completely new to Web2Py and have the following problem:
I am trying to define a database and have the current year as a field which should be automatically filled in using datetime.
But when I try to open the database and look at the entries I get a traceback which I do not understand (see below).
Edit: I think it has to do something with strftime having only one argument. When I am using strftime("%Y%m%d") it is working perfectly. But I really only need the current year.
Thank you
Code
import datetime
db.define_table('orders',
Field("current_year", "string",default=datetime.datetime.now().strftime("%Y"),Label=T('Current Year'), readable=True, writable=False))
Code using IS_DATETIME
After reading the web2py documentation I also tried this:
import datetime
Field('current_year', 'string', label=T('Current Year'), default=IS_DATETIME(format=T('%Y')),readable=True, writable=False))
Traceback:
Traceback (most recent call last): File "/home/PyCatUB/web2py/applications/contacts/controllers/appadmin.py", line 269, in select *fields, limitby=(start, stop)) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/objects.py", line 2020, in select return adapter.select(self.query, fields, attributes) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/adapters/sqlite.py", line 123, in select return super(SQLiteAdapter, self).select(query, fields, attributes) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1296, in select return self._select_aux(sql,fields,attributes) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1253, in _select_aux self.execute(sql) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1388, in execute return self.log_execute(*a, **b) File "/home/PyCatUB/web2py/gluon/packages/dal/pydal/adapters/base.py", line 1382, in log_execute ret = self.get_cursor().execute(command, *a[1:], **b) File "/usr/lib/python2.7/sqlite3/dbapi2.py", line 66, in convert_timestamp datepart, timepart = val.split(" ") ValueError: need more than 1 value to unpack
Upvotes: 0
Views: 822
Reputation: 25536
Field('current_year', 'string', label=T('Current Year'), default=IS_DATETIME(format=T('%Y')), readable=True, writable=False))
Above, IS_DATETIME
is a validator. It is meant to be used with the requires
attribute of the field. You cannot use it with the default
attribute. In that case, when you do an insert, it will simply insert the string representation of that validator object, which will look something like '<gluon.validators.IS_DATETIME object at 0x000002202AC13CC0>'
. That string is obviously not a valid datetime string, so you get an exception when dbapi2.py
attempts to parse it as a timestamp.
If you only want to store the year, a better approach is probably just to use an integer type and store just the year as an integer:
Field('current_year', 'integer', default=request.now.year)
As noted in the comment above, SQLite does not allow you to change the type of an existing field, so if you want to use the name "current_year" for the field, first drop the existing table and let web2py re-create it from scratch with the new field type.
Upvotes: 2
Reputation: 43
Seems like I fixed this. For some reason, web2py did not like the tag "current_year". I changed this to "curyear" because I had the feeling current_year might be a reserved word - and it worked. Could that be the reason?
Upvotes: 0