Andrius
Andrius

Reputation: 21118

Odoo - create_date field changes to bool type?

I noticed one strange thing. I was creating compute method and I needed to use create_date field, but when I tried to strptime, I got error that I use bool field and I need to use string. When I printed it from method, it outputted as False. But when I gone into database and written sql query to get create_date, it showed me normal datetime.

The more interesting thing is, if I define create_date in view, then create_date becomes str type and I can see datetime when calling print. Does anyone know if its intended this way (and why) or it is some kind of bug?

Code that the produced these results:

from openerp import models, fields
from openerp.api import one, depends

class crm_lead(models.Model):
    _inherit = 'crm.lead'

    stage_deadline = fields.Datetime('Stage Deadline', compute="_compute_stage_deadline")


    @one
    @depends('section_id.stage_config_ids', 'stage_id', 'create_date')
    def _compute_stage_deadline(self):
        print type(self.create_date) #prints 'bool' if this field is not defined in view. Prints 'str'
        self.stage_deadline = datetime.now()

If I add this view:

<?xml version="1.0"?>
<openerp>
    <data>         
        <!-- CRM Lead Form View  -->
        <record model="ir.ui.view" id="crm_case_form_view_leads_inherit">
        <field name="name">CRM - Leads Form - Inherit</field>
        <field name="model">crm.lead</field>
        <field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
        <field name="arch" type="xml">
            <field name="priority" position="after">
                <field name="create_date" invisible="1"/>
            </field>
        </field>
        </record>        
    </data>
</openerp>

Then create_date becomes str type. If I delete this view, it goes back in bool type. So this way I can't use create_date if its not defined in a view.

Upvotes: 1

Views: 2481

Answers (2)

sjpatel
sjpatel

Reputation: 184

If the date field is present in the view, but is unfilled (empty) while saving, the functional field will get blank String while using that date in compute method, and if the date field isn't declared at all in the view it will evaluate to False.

Upvotes: 1

Andrius
Andrius

Reputation: 21118

It seems it has to do with unmet dependencies. First two dependencies where not used ('section_id.stage_config_ids', 'stage_id') and it for some reason it evaluated create_date to False too. When I removed those dependencies, it was fixed.

The only thing that I don't understand is why including that field in view converted field back into str even though dependencies were wrong?

Upvotes: 1

Related Questions