Odoo get data from another model with self.env

18,596

Solution 1

Try the next code:

for item in self.pack_move_items:
   warehouse = self.env['stock.warehouse'].browse(item.wh_qc_stock_loc_id.mapped('id'))

Solution 2

After a lot of research I have been able to figure out how this works.

This is the code I have used to get the current warehouse quality location:

wh_qa_location = (self.env["stock.warehouse"].search([("partner_id.id", "=", self.create_uid.company_id.id)])).wh_qc_stock_loc_id

First I will look for the current warehouse the employee is in. Once that is found I will simply get the value of the wh_qc_stock_loc_id

Share:
18,596
Johan Vergeer
Author by

Johan Vergeer

Updated on June 04, 2022

Comments

  • Johan Vergeer
    Johan Vergeer almost 2 years

    For the Odoo warehouse module I have to do a check if all required fields have been filled when quality control wants to transfer the products to stock. At this moment everything works but the location for quality control is currently hardcoded. This means that when someone is using another stock location for quality control they would have to change the code.

    I have searched trough the Odoo documentation and as far as I can see for the new api I have to use self.env instead of self.pool.get. (I added the old code as a comment.) When debugging it seems that stock.warehouse is in self.pool and not in self.env (But I guess this might just be one of those "Odoo" things).

    Second thing is that I have hardcoded the current company_id "1". I think it would be best if that could also be best if that would be a variable.

    I hope someone can help me solve this.

    Thanks in advance

    class stock_transfer_details(models.TransientModel):
        _inherit = "stock.transfer_details"
    
        @api.one
        def do_detailed_transfer(self):
            res = super(stock_transfer_details, self).do_detailed_transfer()
            # Check if all the required lot additional fields have been filled.
            # Else raise warning.
            # TODO Replace hardcoded Quality location by database reference
    
            warehouse = self.env("stock.warehouse").search([("company_id", "=", "1")])
            # self.pool.get("stock.warehouse").browse(cr, uid, item["wh_qc_stock_loc_id"], context=context)
    
            qc_location = warehouse.wh_qc_stock_loc_id
    
            missing_mandatory_fields = []
            if self.picking_source_location_id.id == 14:
                item_ids = self.mapped("item_ids")
                for item in item_ids:
                    additional_fields = item.lot_id.mapped("lot_lot_additional_fields")
                    for field in additional_fields:
                        if field.lot_additional_fields.mandatory and not field.value:
                            if item.lot_id.name not in missing_mandatory_fields:
                                missing_mandatory_fields.append(item.lot_id.name)
    
            if missing_mandatory_fields:
                error_message = "All required fields for the serial numbers must be filled! \n"
                error_message += "Serial numbers: \n"
                for item in missing_mandatory_fields:
                    error_message += item + "\n"
                raise exceptions.Warning(error_message)
    
            return res