跳至内容
菜单
此问题已终结
3 回复
4245 查看

I have 2 model: contract, tenant.

I want to know tenant_id have in contract. 


Model Contract: 

class PmaContract(models.Model):    
​_name = "pma.contract"   
​_inherit = ["mail.thread", "mail.activity.mixin"]   
​_description = "Manage contract"
    ​state = fields.Selection([
​("draft", "Draft"),           
​("to_link", "To link"),
          ("active", "Active"),           
​("expired", "Expired"),           
​("liquidated", "Liquidated")],       
​string="Status",       
​copy=False, index=True, readonly=True, store=True)
    ​lessor = fields.Many2one("res.users",string="Party A (Lessor)",store=True,        required=True,    )
    apartment = fields.Many2one("pma.apartment", string="Apartment name", required=True)
    room = fields.Many2one("pma.apartment.room",string="Room name", domain="[('apartment', '=', apartment)]",        ondelete="restrict",        store=True,        required=True,    )    floor = fields.Many2one(related="room.floor")
    duration_from = fields.Date(string="From", store=True, required=True)    duration_to = fields.Date(string="To", store=True, required=True)
    lessee = fields.Many2one("pma.tenant", string="Representative", store=True, ondelete="restrict", required=True, domain="[('owner', '=', lessor)]",    )
​name = fields.Char(string="Contract ID", compute="_compute_name", store=True)   
​member = fields.Many2many("pma.tenant", store=True, string="Member",domain="[('owner', '=', lessor), ('id', '!=', lessee)]",)

Model Tenant: class PmaTenant(models.Model):   
​_inherit = "pma.tenant"
    ​contract_id = fields.One2many("pma.contract", "lessee", string="Contract", copy=True)
I want to get Tenant(lessee) when contract have status = "active", "liquidated" and don't have in contract.

Please help me ....

形象
丢弃
编写者 最佳答案

I have 2 model: Contract & Room. 


In model room I have a selection field to set state for room. 


They are my model:


- Room:

	​
class PmaApartmentRoom(models.Model):    
​_inherit = "pma.apartment.room"
    contract = fields.One2many("pma.contract", "room", string="Contract")
    state = fields.Selection([
​ ​("available", "Available"),
​ ​("rented", "Rented")],
​ ​string="Status", copy=False,
​ ​index=True, readonly=True, store=True, tracking=True)
  class PmaContract(models.Model):   
​_name = "pma.contract"   
​_inherit = ["mail.thread", "mail.activity.mixin"]   
​_description = "Manage contract"
    state = fields.Selection([
​ ​("draft", "Draft"),           
​ ​("to_link", "To link"), 
        ​("active", "Active"),
        ("expired", "Expired"), 
        ("liquidated", "Liquidated")],       
​ ​string="Status", default="draft", copy=False,       
​ ​ index=True, readonly=True, store=True, tracking=True,)
   
​room = fields.Many2one("pma.apartment.room", string="Room name",        ​ ​domain="[('apartment', '=', apartment)]",        ​ ​ ​ ​ ​ ​ondelete="restrict", store=True, required=True, tracking=True)
State of contract = "active" => state of room = "rented".

State of contract in ["draft", "to_link", "expried", "liquidated"] => state of room = "available"


Please help me ....


形象
丢弃
最佳答案

Hi,

Use Odoo's domain filter to limit your search results to tenants (lessees) with contracts with the status "active" or "liquidated" but no other statuses. These domain requirements can help you achieve this:

tenants_active_liquidated_contracts = self.env['pma.tenant'].search([
    ('contract_id.state', 'in', ['active', 'liquidated']),
    ('contract_id', '!=', False),
])

# You currently have a list of tenants with active or liquidated contracts.
for tenant in tenants_active_liquidated_contracts:
    print("Tenant Name:", tenant.name)  # Replace 'name' with the actual field you want to display


To find tenants who meet the required criteria, we use the search method on the pma.tenant model. We use a domain filter to check if the contract_id field (One2many) is not empty, meaning the tenant has contracts. The state of the contracts (contract_id.state) is also checked to see if it falls within the category of "active" or "liquidated" statuses. The tenants who have contracts with the stated statuses and who don't have contracts with other statuses will be listed for you. The tenant information can then be accessed by iterating through this recordset as needed


Hope it helps

形象
丢弃
最佳答案

Hi,

Try like this in order to get tenants which satisfy your condition mentioned above,

tenant_ids = self.env['pma.tenant'].search(['|',('contract_id.state', 'in', ['active', 'liquidated']),
('contract_id', '=',False)])

Thanks

形象
丢弃
相关帖文 回复 查看 活动
1
3月 23
2402
0
12月 22
3031
0
6月 21
2975
1
12月 20
3994
0
6月 20
5465