콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
6047 화면

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.

베스트 답변

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"/>

 

아바타
취소
작성자 베스트 답변

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?

odoo verison?

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.

베스트 답변

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;)

 

아바타
취소