Search code examples
pythonodoo

Domain for Many2One field not working in odoo


I have an Entity payment (pago) with a Many2One field bill (factura). A bill could have 3 states 'creado', 'pendiente' y 'pagado'. I want to add a domain for the Many2One field: For each payment you can only select a bill in the states 'creado' y 'pendiente'. I have tried with the following code but ii is nt working

class PlanificacionFactura(models.Model):
    _name = 'utepda_planificacion.factura'
    _rec_name = 'numero'
    _description = 'Factura'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    fecha = fields.Date(string='Fecha')
    monto_total = fields.Monetary(string='Monto a pagar', currency_field='currency_id')
    pago_acumulado = fields.Monetary(compute='_compute_pago_acumulado', string='Pago Acumulado' ,currency_field='currency_id')
    currency_id = fields.Many2one('res.currency', string='Moneda', required=True, domain=[('name', 'in', ('USD', 'DOP'))] , default=lambda self: self.env.ref("base.DOP"))
    pago_pendiente = fields.Monetary(compute='_compute_pago_pendiente', string='Pendiente de pago', currency_field='currency_id')

    state = fields.Selection([
        ('creado', 'Creada'),
        ('pendiente','Pagada parcialmente'),
        ('pagado','Pagada')
    ], string='Estado', default='creado', compute='_compute_state' )

    @api.depends('pago_acumulado','monto_total')
    def _compute_state(self):
        for record in self:
            if record.pago_acumulado > 0 and record.pago_acumulado < record.monto_total:
                record.state='pendiente'
            elif record.pago_acumulado == record.monto_total:
                record.state = 'pagado'
            else:
                record.state='creado'

class PagoParcialFactura(models.Model):
    _name = 'utepda_planificacion.pago_parcial'
    _description = 'Permite Realizar el pago de una facura en distintos pagos'

    factura_id = fields.Many2one('utepda_planificacion.factura', string='Factura', required=True, ondelete="cascade", domain=[('state','in',['creado','pendiente'])] )

In the payment view I added the Many2One field

 <group>
     <field name="factura_id" domain="[('state','in',('creado','pendiente'))]"/>

Solution

  • You used a domain based on a non-stored computed field. You should find the following error in the log:

    Non-stored field utepda_planificacion.factura.state cannot be searched.
    

    You can fix that by setting the state store attribute to True or by defining a search method using the state search attribute.

    You can check that at the Odoo documentation:

    Computed fields are not stored by default, they are computed and returned when requested. Setting store=True will store them in the database and automatically enable searching.

    Searching on a computed field can also be enabled by setting the search parameter. The value is a method name returning a search domain.