Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

how to set default date to my date_start and date_end

By
philip
on 9/25/14, 2:33 AM 6,153 views

Hello Expert,

how to set the date_end field to current day +6days, for instance the value would be date_start = 09/25/2014 then date_end = 10/01/2014. Thanks

python----

from datetime import datetime
from dateutil.relativedelta import relativedelta
from osv import osv, fields
from openerp import tools

class test_product(osv.Model):
    _name = "test.product"

    _columns = {

        'date_start': fields.date('Date End'),
        'date_end': fields.date('Date End'),

}

_defaults= {
    'date_start': lambda *a: time.strftime('%Y-%m-%d'),
    'date_end': lambda *a: (datetime.today() + relativedelta(days=6)).strftime('%Y-%m-%d'),
    }

XML---

                <field name="date_start"/>
                <field name="date_end"/>

2

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 9/26/14, 12:53 AM

Also, we can use timedelta 

For the manipulation of date, we can use timedelta to add the day by 5. For more info: https://docs.python.org/2/library/datetime.html#timedelta-objects

from datetime import datetime, timedelta

_defaults={
          'date_start': lambda *a:datetime.now().strftime('%Y-%m-%d'), 
          'date_end': lambda *a:(datetime.now() + timedelta(days=(6))).strftime('%Y-%m-%d'),
}

Thanks sir

philip
on 9/29/14, 1:37 AM
1

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 9/25/14, 3:11 AM

Hello,

 

Please make sure that you've made following imports: 

from datetime import datetime
from dateutil.relativedelta import relativedelta

-than change your code to:

_defaults= {
    'date_start': lambda *a: time.strftime('%Y-%m-%d'),
    'date_end': lambda *a: (datetime.today() + relativedelta(days=6)).strftime('%Y-%m-%d'),
    }

 

regards,

Hi Temur, no default value when heating create, even the date_start field, no value appeared. Please see above code modified Thanks for the response and time really appreciate your help

philip
on 9/25/14, 6:16 AM

You're missing one more import: "import time" you should either add this import, OR use "datetime.today().strftime('%Y-%m-%d')" instead of "time.strftime('%Y-%m-%d')". As you've already used "time" in your question code I didn't mention this import in my answer, I assumed that you had it already.

Temur
on 9/25/14, 9:00 AM

and more correct import for osv will be: "from openerp.osv import osv" instead of: "from osv import osv, fields"

Temur
on 9/25/14, 9:06 AM

sorry, "from openerp.osv import osv, fields"

Temur
on 9/25/14, 9:08 AM

Thanks you so much is working now

philip
on 9/25/14, 9:59 PM

that's nice

Temur
on 9/26/14, 4:12 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/25/14, 2:33 AM
Seen: 6153 times
Last updated: 5/5/16, 4:12 AM