Parcourir la source

修改视图,增加列表,重写类方法_compute_display_name

longyuan il y a 1 mois
Parent
commit
73a556b268
3 fichiers modifiés avec 63 ajouts et 27 suppressions
  1. BIN
      models/__pycache__/hostel.cpython-312.pyc
  2. 21 3
      models/hostel.py
  3. 42 24
      views/hostel.xml

BIN
models/__pycache__/hostel.cpython-312.pyc


+ 21 - 3
models/hostel.py

@@ -1,12 +1,30 @@
-from odoo import models, fields
+from odoo import models, fields, api
 
 
 class Hostel(models.Model):
     _name = 'hostel.hostel'
     _description = 'Information about hostel'
-
+    _order = "id desc, name"
+    _rec_name = 'hostel_code'
+    _rec_names_search = ["name", "hostel_code"]
+    # _table = 'hostel_hostel'
+    
     name = fields.Char(string='Hostel name', required=True)
-    hostel_code = fields.Char(string='Code', required=True)
+    hostel_code = fields.Char(string='代码', required=True)
     street = fields.Char('Street')
     street2 = fields.Char('Street2')
+    city = fields.Char('City')
+    zip = fields.Char('ZIP')
     state_id = fields.Many2one('res.country.state', string='State')
+    country_id = fields.Many2one('res.country', string='Country')
+    phone = fields.Char('Phone')
+    mobile = fields.Char('Mobile')
+    email = fields.Char('Email')
+
+    @api.depends('hostel_code')
+    def _compute_display_name(self):
+        for record in self:
+            name = record.name
+            if record.hostel_code:
+                name = f"{name} ({record.hostel_code})"
+            record.display_name = name

+ 42 - 24
views/hostel.xml

@@ -1,46 +1,64 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <odoo>
     <data>
-        <!-- Hostel Tree View -->
-        <record id="view_hostel_tree" model="ir.ui.view">
-            <field name="name">hostel.hostel.tree</field>
-            <field name="model">hostel.hostel</field>
-            <field name="arch" type="xml">
-                <tree>
-                    <field name="name"/>
-                    <field name="hostel_code"/>
-                </tree>
-            </field>
-        </record>
-
         <!-- Hostel Form View -->
         <record id="view_hostel_form" model="ir.ui.view">
             <field name="name">hostel.hostel.form</field>
             <field name="model">hostel.hostel</field>
             <field name="arch" type="xml">
-                <form>
+                <form string="Hostel">
                     <sheet>
+                        <div class="oe_title">
+                            <h3>
+                                <table>
+                                    <tr>
+                                        <td style="padding-right:10px;">
+                                            <field name="name" required="1" placeholder="Name"/>
+                                        </td>
+                                        <td style="padding-right:10px;">
+                                            <field name="hostel_code" placeholder="Code"/>
+                                        </td>
+                                    </tr>
+                                </table>
+                            </h3>
+                        </div>
                         <group>
-                            <field name="name"/>
-                            <field name="hostel_code"/>
-                            <field name="street"/>
-                            <field name="street2"/>
-                            <field name="state_id"/>
+                            <group>
+                                <label for="street" string="Address"/>
+                                <div class="o_address_format">
+                                    <field name="street" placeholder="Street..." class="o_address_street"/>
+                                    <field name="street2" placeholder="Street 2..." class="o_address_street"/>
+                                    <field name="city" placeholder="City" class="o_address_city"/>
+                                    <field name="state_id" class="o_address_state" placeholder="State" options='{"no_open": True}'/>
+                                    <field name="zip" placeholder="ZIP" class="o_address_zip"/>
+                                    <field name="country_id" placeholder="Country" class="o_address_country" options='{"no_open": True, "no_create": True}'/>
+                                </div>
+                            </group>
+                            <group>
+                                <field name="phone" widget="phone"/>
+                                <field name="mobile" widget="phone"/>
+                                <field name="email" widget="email" context="{'gravatar_image': True}"/>
+                            </group>
                         </group>
                     </sheet>
                 </form>
             </field>
         </record>
 
-        <!-- Hostel Action -->
-        <record id="action_hostel" model="ir.actions.act_window">
-            <field name="name">Hostels</field>
-            <field name="res_model">hostel.hostel</field>
-            <field name="view_mode">tree,form</field>
+        <!-- Hostel Tree View -->
+        <record id="view_hostel_tree" model="ir.ui.view">
+            <field name="name">hostel.hostel.tree</field>
+            <field name="model">hostel.hostel</field>
+            <field name="arch" type="xml">
+                <tree>
+                    <field name="name"/>
+                    <field name="hostel_code"/>
+                </tree>
+            </field>
         </record>
 
         <!-- Hostel Menu -->
         <menuitem id="menu_hostel_root" name="Hostel"/>
         <menuitem id="menu_hostel" name="Hostels" parent="menu_hostel_root" action="action_hostel"/>
     </data>
-</odoo>
+</odoo>