Design Elements

文件中的视图描述形式如下:

Example:
  1. <?xml version="1.0"?>
  2. <openerp>
  3. <data>
  4. [view definitions]
  5. </data>
  6. </openerp>

视图定义包含了以下三种类型的标签:

  • 标签有属性model=“ir.ui.view”,它包含本身的视图定义

  • 标签有属性model=“ir.actions.act_window”,它将动作和这些视图链接起来。

  • 标签,在菜单中创建记录,将他们和动作链接起来。

New:你可以指定组,菜单在menuitem标签中用组属性来访问这些组。

New:你可以使用shortcut标签来加快捷键。

Example:
  1. <shortcut
  2. name="Draft Purchase Order (Proposals)"
  3. model="purchase.order"
  4. logins="demo"
  5. menu="m"/>

你应该加一个id属性在menuitem上

  1. <record model="ir.ui.view" id="v">
  2. <field name="name">sale.order.form</field>
  3. <field name="model">sale.order</field>
  4. <field name="priority" eval="2"/>
  5. <field name="arch" type="xml">
  6. <form string="Sale Order">
  7. .........
  8. </form>
  9. </field>
  10. </record>

priority字段的默认值为16,在没有特别规定的前提下,系统的视图都是最低优先级。

View Types

Tree View

你可以指定列表中包含的列元素和一些细节在列的显示中。search字段不能在这指定,他们显示在form视图中。

  1. <record id="view_location_tree2" model="ir.ui.view">
  2. <field name="name">stock.location.tree</field>
  3. <field name="model">stock.location</field>
  4. <field name="type">tree</field>
  5. <field name="priority" eval="2"/>
  6. <field name="arch" type="xml">
  7. <tree
  8. colors="blue:usage=='view';darkred:usage=='internal'">
  9. <field name="complete_name"/>
  10. <field name="usage"/>
  11. <field
  12. name="stock_real"
  13. invisible="'product_id' not in context"/>
  14. <field
  15. name="stock_virtual"
  16. invisible="'product_id' not in context"/>
  17. </tree>
  18. </field>
  19. </record>

这个例子是一个二维列表,但是你可以通过指点field_parent来显示树结构。这个名字尽管有些误导人,但是该字段必须包含一列所有的child记录。

  1. <record id="view_location_tree" model="ir.ui.view">
  2. <field name="name">stock.location.tree</field>
  3. <field name="model">stock.location</field>
  4. <field name="type">tree</field>
  5. <field name="field_parent">child_ids</field>
  6. <field name="arch" type="xml">
  7. <tree toolbar="1">
  8. <field icon="icon" name="name"/>
  9. </tree>
  10. </field>
  11. </record>

在tree元素中包含下面的属性:

colors

Conditions for applying different colors to items in the list. The default is black.

toolbar

如果你想要一个树结构在单独工具栏区域列出上层的记录,那就把给属性值设为1。当你点击工具栏上的一个记录时,它的所有的后代都会显示在main tree中。二维列表中该值可以忽略

Grouping Elements

Separator

加个分隔线

Example:
  1. <separator string="Links" colspan="4"/>

string属性定义了它的标签,colspan属性定义了他的水平大小(列的个数)。

Notebook

: 用notebooks你可以分配视图字段在不同的标签上(每个由page tag定义)。你可以使用tabpos属性来设置tab:up,down,left,right。

Example:
  1. <notebook colspan="4">....</notebook>

Group

: 对几个列进行分组。

  • colspan: the number of columns to use

  • rowspan: the number of rows to use

  • expand: if we should expand the group or not

  • col: the number of columns to provide (to its children)

  • string: (optional) If set, a frame will be drawn around the group of fields, with a label containing the string. Otherwise, the frame will be invisible.

Example:
  1. <group col="3" colspan="2">
  2. <field name="invoiced" select="2"/>
  3. <button colspan="1" name="make_invoice" states="confirmed" string="Make Invoice"
  4. type="object"/>
  5. </group>

Page

为视图定义新的notebook

Example:
  1. <page string="Order Line"> ... </page>:
  • string: 定义page的名称

Data Elements

Field

attributes for the “field” tag

  • select=”1”: mark this field as being one of the search criteria for

    this resource’s search view. A value of 1 means that the field is included in the basic search, and a value of 2 means that it is in the advanced search.

  • colspan=”4”: the number of columns on which a field must extend.

  • readonly=”1”: set the widget as read only

  • required=”1”: the field is marked as required. If a field is marked as required, a user has to fill it the system won’t save the resource if the field is not filled. This attribute supersede the required field value defined in the object.

  • nolabel=”1”: hides the label of the field (but the field is not hidden in the search view).

  • invisible=”True”: hides both the label and the field.

  • password=”True”: replace field values by asterisks, “*“.

  • string=””: change the field label. Note that this label is also used in the search view: see select attribute above).

  • domain: can restrict the domain.

    • Example: domain=”[(‘partner_id’,’=’,partner_id)]“
  • widget: can change the widget.

    • Example: widget=”one2many_list”

      • one2one_list

      • one2many_list

      • many2one_list

      • many2many

      • url

      • email

      • image

      • float_time

      • reference

  • mode: sequences of the views when switching.

    • Example: mode=”tree,graph”
  • on_change: define a function that is called when the content of the field changes.

    • Example: on_change=”onchange_partner(type,partner_id)”

    • See Events for details.

  • attrs: Permits to define attributes of a field depends on other fields of the same window. (It can be use on page, group, button and notebook tag also)

    • Format: “{‘attribute’:[(‘field_name’,’operator’,’value’),(‘field_name’,’operator’,’value’)],’attribute2’:[(‘field_name’,’operator’,’value’),]}”

    • where attribute will be readonly, invisible, required

    • Default value: {}.

    • Example: (in product.product)

    1. <field digits="(14, 3)" name="volume" attrs="{'readonly':[('type','=','service')]}"/>
  • eval: evaluate the attribute content as if it was Python code (see below for example)

  • default_focus: set to 1 to put the focus (cursor position) on this field when the form is first opened. There can only be one field within a view having this attribute set to 1 (new as of 5.2)

    1. <field name="name" default_focus=”1”/>

例子

Here’s the source code of the view of a sale order object. This is the same object as the object shown on the screen shots of the presentation.

Example:
  1. <?xml version="1.0"?>
  2. <openerp>
  3. <data>
  4. <record id="view_partner_form" model="ir.ui.view">
  5. <field name="name">res.partner.form</field>
  6. <field name="model">res.partner</field>
  7. <field name="type">form</field>
  8. <field name="arch" type="xml">
  9. <form string="Partners">
  10. <group colspan="4" col="6">
  11. <field name="name" select="1"/>
  12. <field name="ref" select="1"/>
  13. <field name="customer" select="1"/>
  14. <field domain="[('domain', '=', 'partner')]" name="title"/>
  15. <field name="lang" select="2"/>
  16. <field name="supplier" select="2"/>
  17. </group>
  18. <notebook colspan="4">
  19. <page string="General">
  20. <field colspan="4" mode="form,tree" name="address"
  21. nolabel="1" select="1">
  22. <form string="Partner Contacts">
  23. <field name="name" select="2"/>
  24. <field domain="[('domain', '=', 'contact')]" name="title"/>
  25. <field name="function"/>
  26. <field name="type" select="2"/>
  27. <field name="street" select="2"/>
  28. <field name="street2"/>
  29. <newline/>
  30. <field name="zip" select="2"/>
  31. <field name="city" select="2"/>
  32. <newline/>
  33. <field completion="1" name="country_id" select="2"/>
  34. <field name="state_id" select="2"/>
  35. <newline/>
  36. <field name="phone"/>
  37. <field name="fax"/>
  38. <newline/>
  39. <field name="mobile"/>
  40. <field name="email" select="2" widget="email"/>
  41. </form>
  42. <tree string="Partner Contacts">
  43. <field name="name"/>
  44. <field name="zip"/>
  45. <field name="city"/>
  46. <field name="country_id"/>
  47. <field name="phone"/>
  48. <field name="email"/>
  49. </tree>
  50. </field>
  51. <separator colspan="4" string="Categories"/>
  52. <field colspan="4" name="category_id" nolabel="1" select="2"/>
  53. </page>
  54. <page string="Sales &amp; Purchases">
  55. <separator string="General Information" colspan="4"/>
  56. <field name="user_id" select="2"/>
  57. <field name="active" select="2"/>
  58. <field name="website" widget="url"/>
  59. <field name="date" select="2"/>
  60. <field name="parent_id"/>
  61. <newline/>
  62. </page>
  63. <page string="History">
  64. <field colspan="4" name="events" nolabel="1" widget="one2many_list"/>
  65. </page>
  66. <page string="Notes">
  67. <field colspan="4" name="comment" nolabel="1"/>
  68. </page>
  69. </notebook>
  70. </form>
  71. </field>
  72. </record>
  73. <menuitem
  74. action="action_partner_form"
  75. id="menu_partner_form"
  76. parent="base.menu_base_partner"
  77. sequence="2"/>
  78. </data>
  79. </openerp>

The eval attribute

The eval attribute evaluate its content as if it was Python code. This allows you to define values that are not strings.

一般情况下,内容中标签一直作为字符串来计算。

Example 1:

  1. <field name="value">2.3</field>

2.3不是float 2.3而是字符串2.3

Example 2:

  1. <field name="value">False</field>

This will evaluate to the string ‘False’ and not the boolean False

如果你想要计算float,boolean或是其他类型,除了string,可以使用eval属性:

  1. <field name="value" eval="2.3" />
  2. <field name="value" eval="False" />

Button

: add a button using the string attribute as label. When clicked, it can trigger methods on the object, workflow transitions or actions (reports, wizards, …).

  • string: define the button’s label

  • confirm: a message for an optional confirmation pop-up, if needed. Eg: confirm=”Are you sure?”

  • name: the name of the function to call when the button is pressed. In the case it’s an object function, the corresponding function must take 4 arguments: cr, uid, ids, context:

    • cr is a database cursor

    • uid is the ID of the user who clicked the button

    • ids is the list of record ID on which the function is called (usually just the one on which the form is open)

    • context is a dictionary of context values passed from the view

  • states: a comma-separated list of states (from the state field or from the workflow) in which the button must appear. If the states attribute is not given, the button is always visible.

  • type: this attribute can have 3 values

    • “workflow” (default): the button will send a workflow signal

    • “object”: the button will call a method of the object

    • “action”: the button will call an action

  • default_focus: set to 1 to make this button the default button, highlighted and activated directly if you press enter on the form. There can only be one button within a view having this attribute set to 1 (new as of 5.2)
Example:
  1. <button name="order_confirm" states="draft" string="Confirm Order" icon="gtk-execute"/>
  2. <button name="_action_open_window" string="Open Margins" type="object" default_focus=”1”/>

Label

用label来加个标题。

Example:
  1. <label string="Test"/>

New Line

如果视图的所有列不能全部放进一行是强制换行。

Example:
  1. <newline/>