0

I have an Inventory module. I want to check db before creating data. Check for reference code. If ref_code already exist then cancel the creation.

This is my .py file:

    ref_code = fields.Char(string="Referans Numarası: ", required=True, tracking=True,       related="products_id.ref_no")
    #product_name = fields.Char(string="Ürün Adı: ", required=True, tracking=True)
    product_description = fields.Char(string="Ürün Tanımı: ", tracking=True,)
    teslim_alan = fields.Char(string="Teslim Alan: ", required=True, tracking=True,)
    teslim_eden = fields.Char(string="Teslim Eden: ", required=True, tracking=True,)
    quantity = fields.Float(string="Miktar: ", required=True, tracking=True)
    price = fields.Float(string="Fiyat(€): ", required=True, tracking=True, related="products_id.unit_price")
    unit_price = fields.Float(string="Birim Fiyat(€): ", compute="_unitPriceCalcuteFunc")
    scrap_quantity = fields.Float(string="Hurdaya Taşınacak Miktar: ")

    warehouse_id = fields.Many2one('ware.houses', string='Depo Adı: ')
    products_id = fields.Many2one('products', string='Ürün: ')

    state = fields.Selection([
        ('unapproved', 'Çıkış İçin Onay Verilmedi.'),
        ('approved', 'Çıkış İçin Onay verildi.')], string="Status", default="unapproved", tracking=True)

    cikis_line_ids = fields.One2many('inventory.out.report.lines', 'inventory_id', string='Çıkış Listesi')

    @api.model
    def create(self, values):
        global count
        count = 0
        value = self.env['inventory.menu'].search([])
        for record in values:
            for v in value:
                print(v.ref_code, record.ref_code)
                if(v.ref_code == record.ref_code):
                    count += 1
                    return print("Zaten Var!")
        if(count == 0):
            return super(InventoryMenu, self).create(values)

I can find the all data in db. It is ok. But the current data is not exist, i can't use it. I need the compare current data with db data. How can i do it? Many thanks..

Enes Kara
  • 81
  • 6
  • 4
    Add [unique](https://github.com/odoo/odoo/blob/9c19460ce7fb1a16cfeed607e474e273f5ce690b/addons/account/models/account_journal.py#L197-L199) constraint to `ref_code` field – Kenly Jul 26 '22 at 12:34
  • How can i add that? I couln't find – Enes Kara Jul 26 '22 at 14:12
  • 2
    You can use the [_sql_constraints](https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html#odoo.models.BaseModel._sql_constraints) like in the link in my comment, – Kenly Jul 26 '22 at 14:21
  • 1
    You need to add constraint and inside it validation error Just read this article https://www.cybrosys.com/blog/python-model-constraints-odoo-13 – omar ahmed Jul 26 '22 at 14:28

2 Answers2

1
@api.model
 def create(self, values):
     global count
     count = 0
     value = self.env['inventory.menu'].search([])
     for v in value:
       if(v.ref_code == values['ref_code']):
          count += 1
          return print("Zaten Var!")
     if(count == 0):
        return super(InventoryMenu, self).create(values)
    enter code here
1

You can search for the specific ref_code. Just didn't do something like search([]) cause it's contrproductive.

    @api.model
    def create(self, values):
        ref_codes_count = self.env['inventory.menu'].search(
          [("ref_code", "=", values.get("ref_code"))],
          count=True,
        )
        if not ref_codes_count:
          return super(InventoryMenu, self).create(values)

or you can try to use something like this

@api.model
def create(self, values):
    self.env.cr.execute(
        "SELECT COUNT(*) FROM module_name.model_name WHERE ref_code='%s'" %
        values.get("ref_code")
    )
    ref_codes_count = self.env.cr.fetchall()[0]
    if not ref_codes_count:
          return super(InventoryMenu, self).create(values)
Artem Strogin
  • 176
  • 1
  • 6