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

How to populate dropdown from the database?

By
HateCamel
on 2/1/15, 11:57 AM 889 views

Hi people,

just started learning python and openerp and I am pretty lousy so far, but I am trying.

My question is how to populate selection field with data from the database?

I have a 2 simple tables one with data of the employee(name, surname, emal, department) and the other called departments which only has department names.

When creating a new employee I would like to select the department from the dropdown populated with the departments from my other table. Pretty basic stuff.

class djelatnik(osv.osv):

    
    _inherit= 'department
    _name = 'employee'

    def _fetch_departments(self, cr, uid, ids, context = None):
        res=[]
        cr.execute('select id , department from departments')
        departments=cr.fetchall()
        for dep in departments:
            res.append((dep.id,dep.department))
        return res
    

    _columns = {
        'name': fields.char('name',size=30, required=True, help='name'),
        'surname': fields.char('surname', size=30, required=True, help='surname'),
     
        'odjel': fields.selection(_fetch_departments ,'Departments')
        
    }   

What am I doing wrong?

Ansewer updated. You change code and xml.

zbik
on 2/1/15, 4:36 PM
0

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 2/2/15, 3:07 AM

Heh, the easiest way to achieve that is to make the field many2one instead of selection.. .
If you insist on look and feel of selection field, just add widget="selection" in you xml view definition for that field.. 
 

But somethinng else seems to be the problem.. 
You have a models for employee and for department in odoo already.. 
wich you can modify/extend to your needs.. 

YOur mistake is making the wrong inheritance.. 
model djelatnik should inherit hr_employee, and model odjel should inherit hr_department
your way if mixed sou you get apples and oragnes mixed.. 

hope it helps a bit;)

 

0
HateCamel
On 2/1/15, 12:58 PM

Thanks mate but that way I get department,1 and so on. How do I get to display the name and not the id?

 

I am using v7.

 

<?xml version="1.0"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="view_employee_form">
            <field name="name">employee.form</field>
            <field name="model">employee</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="employee">                   
                    <field name="name" select="1"/>
                    <field name="surname" select="2"/>
                    <field name="department" select='0'/>
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_employee_tree">
            <field name="name">employee.tree</field>
            <field name="model">employee</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="employee">
                    <field name="name"/>
                    <field name="surname"/>
                     <field name="department" />
                </tree>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_djelatnik">
            <field name="name">employee</field>
            <field name="res_model">employee</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{"search_default_type_date":0}</field>
        </record>
        <menuitem name="HR/HR" id="menu_employee" action="action_employee"/>
        <menuitem name="employee" id="menu_employeek_employee_item" parent="menu_employee" action="action_employee"/>

        <record model="ir.ui.view" id="view_department_form">
            <field name="name">department.form</field>
            <field name="model">departmentl</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="department">                   
                    <field name="department" select="1"/>
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_department_tree">
            <field name="name">department.tree</field>
            <field name="model">department</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="odjel">
                    <field name="department"/>
                </tree>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_department">
            <field name="name">department</field>
            <field name="res_model">department</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>
        <menuitem name="department" id="menu_department_department_item" parent="menu_department" action="action_odjel"/>
    </data>
</openerp>

 

your xml?

zbik
on 2/1/15, 1:08 PM

odoo verison?

zbik
on 2/1/15, 1:12 PM

If you are getting department,1, etc. most probably it is because the department model does not have field that is named 'name'. Or, alternatively you can set the model's _rec_name attribute to a field that you want to display. Or, alternatively you can develop the method name_get() to set the display name.

Ivan
on 2/1/15, 9:01 PM
0

zbik

--zbik--
5976
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 2/1/15, 12:32 PM

Example classes, tables, and selections, between tables in odoo.

UPDATED: with xml

    class department(osv.osv):
        _name = 'department'

        _columns = {
                 'name': fields.char('Department', size=32, required=True),
        }

    class djelatnik(osv.osv):
        _name = 'employee'

        _columns = {
            'name': fields.char('name',size=30, required=True, help='name'),
            'surname': fields.char('surname', size=30, required=True, help='surname'),
            'odjel': fields.many2one('department' ,'Departments')           
        }   

        <record model="ir.ui.view" id="view_employee_form">
            <field name="name">employee.form</field>
            <field name="model">employee</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="employee">                   
                    <field name="name" select="1"/>
                    <field name="surname" select="2"/>
                    <field name="odjel" select='0'/>
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_employee_tree">
            <field name="name">employee.tree</field>
            <field name="model">employee</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="employee">
                    <field name="name"/>
                    <field name="surname"/>
                     <field name="odjel"/>
                </tree>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_employee">
            <field name="name">employee</field>
            <field name="res_model">employee</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{"search_default_type_date":0}</field>
        </record>
        <menuitem name="HR/HR" id="menu_employee" action="action_employee"/>
        <menuitem name="employee" id="menu_employee_employee_item" parent="menu_employee" action="action_employee"/>

        <record model="ir.ui.view" id="view_department_form">
            <field name="name">department.form</field>
            <field name="model">department</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="department">                   
                    <field name="name" select="1"/>
                </form>
            </field>
        </record>
        <record model="ir.ui.view" id="view_department_tree">
            <field name="name">department.tree</field>
            <field name="model">department</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="odjel">
                    <field name="name"/>
                </tree>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_department">
            <field name="name">department</field>
            <field name="res_model">department</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>
        <menuitem name="department" id="menu_department_department_item" parent="menu_employee" action="action_department"/>

 

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: 2/1/15, 11:57 AM
Seen: 889 times
Last updated: 3/16/15, 8:10 AM