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.

0

Default value for new object - Odoo 8

By
Alejandro Perez Cosio
on 4/8/15, 8:37 AM 4,410 views

Dear all

I would want to have the same behavior as when you click on

Sale > Customers > Create (> the customer checkbox for the partner is automatically selected)

Or

Purchase > Suppliers > Create (> the supplier checkbox for the partner is automatically selected)

I added a new checkbox "Doctor" to partner, and I would want to set it as default if I click "Create" at my new menu at Medical.

Medical > Doctor > Create (> the doctor checkbox should be checked)

Thank you in advance

Best regards!

1

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 4/8/15, 8:48 AM

you should set it's default value to True, then the checkbbox will appear selected in new object

Hello Temur, I want it to set doctor = True only if I create a new partner from my new menuitem action.

Alejandro Perez Cosio
on 4/8/15, 9:58 AM

ok then. in such case you can set default value for doctor field using function. my answer is still valid in this conditions.

Temur
on 4/8/15, 10:04 AM

you can implement function that assigns default value as follows:

def compute_default_value_of_doctor(self):
    return self.env.context.get('doctor_default', False)
Temur
on 4/8/15, 10:06 AM

in your action set 'doctor_default' in context....

Temur
on 4/8/15, 10:09 AM

of course you've to add

default=compute_default_value_of_doctor
to your "Doctor" fields definition.
Temur
on 4/8/15, 10:14 AM

Works great! Thank you!

Alejandro Perez Cosio
on 4/8/15, 11:20 AM
2

This solution worked as Temur said at the comments: 

def compute_default_value_of_doctor(self):
return self.env.context.get('doctor_default', False)

# Fields
doctor = fields.Boolean(string='Is doctor?', default=compute_default_value_of_doctor)

For the specific action view for the doctor menuitem add this:

<field name="context">{"default_doctor": True}</field>

Update: There is no need to write the python code, just add "default_" at the context field before the name of the attribute and it will work.

Regards!

upvote for the "default_" prefix ;)

Temur
on 4/14/15, 3:14 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

2 follower(s)

Stats

Asked: 4/8/15, 8:37 AM
Seen: 4410 times
Last updated: 3/14/16, 10:24 AM