Skip to Content
Menu
This question has been flagged
2 Replies
675 Views

Hello All,

I have class called  _name = "stock.request.order"

on this class i have a fields :

warehouse_id = fields.Many2one('stock.warehouse', string="Warehouse",)
warehouse_user_on_requist = fields.Many2one('res.users', string="User ID", default=lambda self: self.env.user)

and i have inherit the stock.warehouse and add on field:

class StockWarehouse(models.Model):
_inherit = "stock.warehouse"
warehouse_user = fields.Many2one('res.users', string="Warehouse User")

now what i want to do is that i will register a user on each warehouse warehouse_user

and on the class "stock.request.order" 

i will call all the warehouses on  warehouse_id filed but only if the current user is registered on the warehouse 

it is simple a filter for the many2one filed that shouwing the requord if the current user is the same one that on the warehouse_user filed on the stock.warehouse class


i have try many thing but its not working for me 

 using def 

warehouse_id = fields.Many2one('stock.warehouse', string="Warehouse",)
warehouse_user = fields.Many2one('res.users', string="User ID", default=lambda self: self.env.user)

def _calc_user(self):
userr_id = 0
if self.warehouse_id:
userr_id = self.warehouse_id.warehouse_user.id
return {"domain": {'warehouse_id': [('id', '=', userr_id)]}}


not working 


using domain

warehouse_id = fields.Many2one('stock.warehouse', string="Warehouse", domain="[('warehouse_user', '=', warehouse_user)]")
warehouse_user = fields.Many2one('res.users', string="User ID", default=lambda self: self.env.user)


not working 

please help 





Avatar
Discard
Author Best Answer

will i have try your code but it is not working for me i find the solution on this code:

@api.model
def default_get(self, fields):
res = super().default_get(fields)
warehouse = None
if
"warehouse_id" not in res and res.get("company_id"):
warehouse = self.env["stock.warehouse"].search(
[("company_id", "=", res["company_id"]),
("warehouse_user_in_warehouse", "=", res["warehouse_user"])], limit=1
)
if warehouse:
res["warehouse_id"] = warehouse.id
res["location_id"] = warehouse.lot_stock_id.id
return res


Avatar
Discard
Best Answer

Hello Mohamed AIObaid,

Please find code in comment.

I hope this will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

You can used below example:

.py file

def _calc_user(self):
userr_id = False
if self.warehouse_id:
userr_id = self.warehouse_id.warehouse_user.id
return {"domain": {'warehouse_id': [('warehouse_user', '=', userr_id)]}}

OR, Add domain in xml file for the field..
.xml file

<field name="warehouse_id" domain="[('warehouse_user', '=', warehouse_user)]"/>