简体   繁体   English

遍历odoo 14中的对象

[英]Looping through objects in odoo 14

I'm new to odoo development.I'd like to create a function on my model 'hotel.room' to show number of rooms created which is of type " Dormitory".However, I have no idea of how to do it.我是odoo开发的新手。我想在我的model'hotel.room'上创建一个function,以显示创建的“宿舍”类型的房间数。但是,我不知道该怎么做。 Please help me if you know how I could do it.如果您知道我该怎么做,请帮助我。 When I create a new room and select the bedtype as "Dormitory " It should show the number of rooms that is already created as type "Dormitory".当我创建一个新房间和 select 时,床型为“宿舍” 它应该显示已经创建为“宿舍”类型的房间数量。 The count should be visible in the form view.计数应该在表单视图中可见。

My Model:我的 Model:

class HotelRoom(models.Model):
_name = 'hotel.room'
_description = 'hotel room'
_rec_name = 'room_number'

room_number = fields.Integer('Room Number', required=True)
currency_id = fields.Many2one('res.currency', string='Currency')
room_rent = fields.Monetary('Room Rent')
tag = fields.Many2many('hotel.tags', string='Facilities')

bed_type = fields.Selection([
    ('single', 'Single'),
    ('double', 'Double'),
    ('dormitory', 'Dormitory')
], required=True, default='other')

Form view: I need to show the count here.表单视图:我需要在此处显示计数。

<record id="room_create_form" model="ir.ui.view">
<field name="name">hotel.room.form</field>
<field name="model">hotel.room</field>
<field name="arch" type="xml">
  <form>
     <sheet>
        <group>
           <group>
              <field name="room_number"/>
              <field name="currency_id"/>
              <field name="room_rent"/>
           </group>
           <group>
              <field name="bed_type"/>
              <field name="tag" widget="many2many_tags"/>
           </group>
        </group>
     </sheet>
  </form>
   </field>
</record>

You can do like create a method which will call on change of field bed_type您可以创建一个方法来调用字段bed_type on change

@api.onchange('bed_type')
def count_dormitory(self)
    total_bed = self.env['hotel.room'].search_count([('bed_type', '=', 'dormitory')])

    return total_bed

But here is a question where you want to display your total count in some other field if yes then create another field and assign this field a value like following.但是这里有一个问题,如果是,您想在其他字段中显示您的总数,然后create another field并为该字段分配一个值,如下所示。

@api.onchange('bed_type')
def count_dormitory(self)
    total_bed = self.env['hotel.room'].search_count([('bed_type', '=', 'dormitory')])

    self.another_field =  total_bed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM