Convert Created Date format from database value

Asked by John Chen

Hi

I need to convert the created date format from database value.

My Code :
# -*- encoding: utf-8 -*-
from osv import osv, fields
import os

class purchase_orders_creator(osv.osv):
    _name = "purchase.order"
    _inherit = 'purchase.order'
    _columns = {
        'create_uid': fields.many2one('res.users', 'Created by', readonly=True, select=True),
        'write_uid': fields.many2one('res.users', 'Modified by', readonly=True, select=True),
        'create_date': fields.datetime('Date Created', readonly=True, select=True, help="Date on which purchase order has been created"),
        'write_date': fields.datetime('Modified Date', readonly=True, select=True),
    date(write_date).isoformat() ==
 }
purchase_orders_creator()

using this code, i can get the create_date value from the database but it shows 2011-07-06 12:35:31.08
and then place this field and value in the purchase order form.

I need to change the date format to become Y-M-D only 2011-07-06

is there any way to change the format? what python code should i add?

Thanks OpenErp Community.

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Web Client Edit question
Assignee:
No assignee Edit question
Solved by:
digitalsatori(Shine IT)
Solved:
Last query:
Last reply:
Revision history for this message
Graeme Gellatly (gdgellatly) said :
#1

Well firstly, there really is no reason to create those fields. Those
fields are created automatically. In fact by also manually declaring them
you may introduce unpredictable behaviour.

Now on to your question. The answer is, it depends, and lots of ways. Do
you want a static conversion, i.e. always to Y-M-D or to honour the users
locale settings?

Anyway the common way is to use the time library and specifically the
functions strptime and strftime, although isoformat is another good way of
converting a time string to a time tuple. You end up with code that looks
something like time.strftime('%Y-%m-%d', time.strptime(create_date,
'%Y-%m-%d %H:%M:%S')) You will need import time at the top of your file.

First, although last in the code you use strptime to convert the string to a
time tuple, then you convert the tuple back to a string using strftime.

Fur further reference just look at the python standard library under time.
 You will also probably need to do further reading on the perm_read function
of the orm.

On Wed, Jul 6, 2011 at 4:41 PM, John Chen <
<email address hidden>> wrote:

> New question #163893 on OpenERP Web Client:
> https://answers.launchpad.net/openobject-client-web/+question/163893
>
> Hi
>
> I need to convert the created date format from database value.
>
> My Code :
> # -*- encoding: utf-8 -*-
> from osv import osv, fields
> import os
>
> class purchase_orders_creator(osv.osv):
> _name = "purchase.order"
> _inherit = 'purchase.order'
> _columns = {
> 'create_uid': fields.many2one('res.users', 'Created by',
> readonly=True, select=True),
> 'write_uid': fields.many2one('res.users', 'Modified by',
> readonly=True, select=True),
> 'create_date': fields.datetime('Date Created', readonly=True,
> select=True, help="Date on which purchase order has been created"),
> 'write_date': fields.datetime('Modified Date', readonly=True,
> select=True),
> date(write_date).isoformat() ==
> }
> purchase_orders_creator()
>
> using this code, i can get the create_date value from the database but it
> shows 2011-07-06 12:35:31.08
> and then place this field and value in the purchase order form.
>
> I need to change the date format to become Y-M-D only 2011-07-06
>
> is there any way to change the format? what python code should i add?
>
> Thanks OpenErp Community.
>
> --
> You received this question notification because you are a member of
> OpenERP Committers, which is an answer contact for OpenERP Web Client.
>

Revision history for this message
John Chen (john-chen2011) said :
#2

Thanks Graeme Gellatly

can u teach me how to write the code?
i wanted to convert the existing date value from the posgresql database.

Normally Created_date and Created_uid do not show in purchase order form, and i use the inherit method that i type up there to call the value from the database to show it on the purchase order form. But the problems is i wanted to change the date format from the database value that i called.

Thanks Graemen Gellatly :D

Revision history for this message
Best digitalsatori(Shine IT) (digitalsatori) said :
#3

don't have to change in the database, modify the view definition as below:
<field name="create_date" widget="date"/>

Tony Gu

On Wed, Jul 6, 2011 at 2:26 PM, John Chen <
<email address hidden>> wrote:

> Question #163893 on OpenERP Web Client changed:
> https://answers.launchpad.net/openobject-client-web/+question/163893
>
> Status: Answered => Open
>
> John Chen is still having a problem:
> Thanks Graeme Gellatly
>
> can u teach me how to write the code?
> i wanted to convert the existing date value from the posgresql database.
>
> Normally Created_date and Created_uid do not show in purchase order
> form, and i use the inherit method that i type up there to call the
> value from the database to show it on the purchase order form. But the
> problems is i wanted to change the date format from the database value
> that i called.
>
> Thanks Graemen Gellatly :D
>
> --
> You received this question notification because you are a member of
> OpenERP Committers, which is an answer contact for OpenERP Web Client.
>

Revision history for this message
John Chen (john-chen2011) said :
#4

Thanks digitalsatori, that solved my question.

Revision history for this message
John Chen (john-chen2011) said :
#5

Thanks digitalsatori. That really help me a lot :D
do you have facebook? who knows we can share our knowledge.