Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odpovědi
4222 Zobrazení

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 ....

Avatar
Zrušit
Autor Nejlepší odpověď

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 ....


Avatar
Zrušit
Nejlepší odpověď

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

Avatar
Zrušit
Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
bře 23
2377
0
pro 22
3003
0
čvn 21
2933
1
pro 20
3956
0
čvn 20
5438