İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
6060 Görünümler


class AccountInvoice(models.Model): _inherit = ["account.invoice"]

duplication_number = fields.Char(string="Duplication Number")

@api.multi def check_fields(self): print self.id invoices = self.env['account.invoice'].search([]) for record in invoices: if record.duplication_number != self.duplication_number: raise UserError(_("You can't save this record, the same Vendor Bill can only be used one time."))

@api.multi def write(self, vals): self.check_fields() res = super(AccountInvoice, self).write(vals) return res


I don't understand why my "self" in check_fields(self) is empty everytime.

I can't get the actual value of the field: duplication_number in self. The return of self is account.invoice(), everytime.

I need to get the actual value of my field "duplication_number" when I want to save it. It should check if this field has the same value as any other field in account.invoice



Avatar
Vazgeç
En İyi Yanıt

Hi,

Are you looking to check whether such a number is already saved in the db ?

If so, easily you can do it with using the sql constrains,

_sql_constraints = [    ('unique_duplication_number', 'unique (duplication_number)', 'Show message here!'),]


Or else you can do it using the constrains,





Thanks

Avatar
Vazgeç
Üretici

the problem now is, that invoice_rec is everytime true...

The _sql_constraints method should be used, it is the most secure way as the rule is implemented at the database level.

Also, @api.constrains methods takes recordsets, therefore a 'for rec in self:' loop should wrap this otherwise it will fail if multiple records are added.

İlgili Gönderiler Cevaplar Görünümler Aktivite
2
Eyl 18
4823
2
Eyl 17
7973
1
Haz 17
4963
5
Eyl 20
62308
0
Tem 19
12332