ผลต่างระหว่างรุ่นของ "Openerp/histest1"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
แถว 36: แถว 36:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
อย่างไรก็ตาม มอดูล histest จะถูกมองเป็นแพกเก็จของไพทอน ซึ่งจำเป็นต้องมีไฟล์ <code>__init__.py</code> อยู่ในไดเรคตอรีเดียวกันด้วย บนระบบยูนิกซ์สามารถใช้คำสั่ง touch ได้ดังนี้
+
อย่างไรก็ตาม มอดูล histest จะถูกมองเป็นแพกเก็จของไพทอน ซึ่งจำเป็นต้องมีไฟล์ <code>__init__.py</code> อยู่ในไดเรคตอรีเดียวกันด้วย โดยมีคำสั่ง import หนึ่งบรรทัดดังนี้
  
touch __init__.py
+
<syntaxhighlight lang="python">
 
+
import histest
หรือใช้เอดิเตอร์สร้างและบันทึกไฟล์เปล่าชื่อ <code>__init__.py</code> ขึ้นมาก็ได้เช่นกัน
+
</syntaxhighlight>
  
 
== สร้างโมเดลเก็บข้อมูล ==
 
== สร้างโมเดลเก็บข้อมูล ==

รุ่นแก้ไขเมื่อ 03:12, 27 มีนาคม 2557

สร้างมอดูล histest

ในไดเรคตอรี openerp/addons/ สร้างไดเรคตอรีชื่อ histest

mkdir histest
cd histest

จากนั้นสร้างไฟล์ชื่อ __openerp__.py ขึ้นมาเพื่อระบุ meta-data ของมอดูลในรูปดิคชันนารีของไพทอนดังนี้

{
    'name': 'HIS Test',
    'version': '0.1',
    'category': 'Tools',
    'description': """
This is a training module for new HIS
=====================================
 
""",
    'author': 'Chaiporn',
    'website': 'http://openerp.com',
    'summary': 'HIS Test',
    'sequence': 9,
    'depends': [],
    'data': [
        'histest_view.xml',
    ],
    'demo': [],
    'test': [],
    'css': [],
    'images': [],
    'installable': True,
    'application': True,
    'auto_install': False,
}

อย่างไรก็ตาม มอดูล histest จะถูกมองเป็นแพกเก็จของไพทอน ซึ่งจำเป็นต้องมีไฟล์ __init__.py อยู่ในไดเรคตอรีเดียวกันด้วย โดยมีคำสั่ง import หนึ่งบรรทัดดังนี้

import histest

สร้างโมเดลเก็บข้อมูล

สร้างไฟล์ชื่อ histest.py เพื่อประกาศโมเดลสำหรับเก็บข้อมูลดังนี้

# encoding=utf-8

from osv import osv, fields

class HISPatient(osv.Model):
    _name = "histest.patient"

    _columns = {
            'title' : fields.char(
                size=50,
                string=u'คำนำหน้าชื่อ',
                required=True),

            'first_name' : fields.char(
                size=50,
                string=u'ชื่อ',
                required=True
                ),

            'last_name' : fields.char(
                size=50,
                string=u'นามสกุล',
                required=True
                ),
            }

เตรียมหน้าจอแสดงผล (view)

สร้างไฟล์ชื่อ histest_view.xml โดยมีโค้ดดังนี้

<?xml version="1.0" encoding="utf-8"?>
<openerp>
  <data>

    <record model="ir.ui.view" id="view_histest_patient_tree">
      <field name="name">histest.patient.tree</field>
      <field name="model">histest.patient</field>
      <field name="arch" type="xml">
        <tree string="Patient">
          <field name="title"/>
          <field name="first_name"/>
          <field name="last_name"/>
        </tree>
      </field>
    </record>

    <record model="ir.ui.view" id="view_histest_patient_form">
      <field name="name">histest.patient.form</field>
      <field name="model">histest.patient</field>
      <field name="arch" type="xml">
        <form string="Patient" version="7.0">
          <group>
            <field name="title"/>
            <field name="first_name"/>
            <field name="last_name"/>
          </group>
        </form>
      </field>
    </record>

    <record model="ir.actions.act_window" id="action_histest_patient">
      <field name="name">Patient</field>
      <field name="res_model">histest.patient</field>
      <field name="view_type">form</field>
      <field name="view_mode">form,tree</field>
    </record>

    <menuitem name="HIS" id="menu_histest_top" 
      parent="" sequence="1" 
      action="histest.action_histest_patient" />

  </data>
</openerp>