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

4
  • 4
    Add unique constraint to ref_code field Commented Jul 26, 2022 at 12:34
  • How can i add that? I couln't find Commented Jul 26, 2022 at 14:12
  • 2
    You can use the _sql_constraints like in the link in my comment, Commented Jul 26, 2022 at 14:21
  • 1
    You need to add constraint and inside it validation error Just read this article cybrosys.com/blog/python-model-constraints-odoo-13 Commented Jul 26, 2022 at 14:28

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.