Skip to Content
Menu
This question has been flagged
5 Replies
7947 Views

Hi ! First sorry for my poor English !

Here's my problem : I'm trying to create data in a controller like this. It works and my employees are created in the employees module.

@http.route('/employees/api/employee', type='json', auth='user', methods=['POST'], cors='*', csrf=False)
def create_employee (self, **kwargs):
data = json.loads(request.httprequest.data)

employee = request.env['hr.employee'].sudo().search([('work_email', '=', data.get('work_email'))])
if employee:
return {'message': 'already exists'}
else:
id = request.env['hr.employee'].sudo().create({
'name': data.get('name'),
'work_email': data.get('email'),
}).id
return {'message': 'Employee {} created'.format(id)}

Now, I want to insert personnal address for this employee, but I don't know how to do that ... In the model, I see that it's in the address_home_id field, but I don't know how to create the address and attach the address_home_id to the newly created address !

Thanks you very much !


PS : I'm on odoo 12 ;)  

Avatar
Discard

Controllers in odoo: https://goo.gl/gPjPas

Best Answer

Hello Thomas,

Firstly, you need to create your address inside res.partner model; then you can assign its inside employee record.

You can refer to the below mentioned code:

@http.route('/employees/api/employee', type='json', auth='user', methods=['POST'], cors='*', csrf=False)
def create_employee (self, **kwargs):
    data = json.loads(request.httprequest.data)
    employee = request.env['hr.employee'].sudo().search([('work_email', '=', data.get('work_email'))])
    if employee:
        return {'message': 'already exists'}
    else:
        # Create Home Address
        home_address = request.env['re.partner'].sudo().create({
            'name': 'Name',
            'street': 'Street',
            'street2': 'Street2',
            'city': 'City',
            'state_id': state_id,
            'country_id': country_id,
            'zip': '201301',
            'phone': '99999999',
            'email': 'email@email.com',
        })
        if home_address:
            home_address = home_address.id
            employee = request.env['hr.employee'].sudo().create({
                'name': data.get('name'),
                'work_email': data.get('email'),
                'address_home_id':home_address
            })
            if employee:
                employee = employee.id
        return {'message': 'Employee {} created'.format(employee)}
Thanks 
Anisha Bahukhandi
Technical Content Writer
Webkul Softwares
Avatar
Discard
Author Best Answer
thank you very much to both of you it works!
Have a nice day and thanks again !
Avatar
Discard

Hello Thomas,

It's great that your query is resolved and you are satisfied with my assistance for the question you asked in Odoo Forum.

I would really appreciate if you can take out a few minutes out of your busy schedule and put a google review for me here -

https://www.google.com/search?q=webkul&oq=webkul+&aqs=chrome..69i57j69i60l3j0l2.4182j0j7&sourceid=chrome&ie=UTF-8#lrd=0x390ce561c5555555:0xcfb40ae166ce6c21,1,

A nice review from your side will motivate me and my team to provide exceptional assistance.

I hope you can manage to provide few minutes for this :)

Regards

Anisha Bahukhandi

Best Answer

Hi,

I think you can create home address and assign to employee. Please check below.

@http.route('/employees/api/employee', type='json', auth='user', methods=['POST'], cors='*', csrf=False)
def create_employee (self, **kwargs):
data = json.loads(request.httprequest.data)
address_id = False

address = request.env['res.partner'].sudo().search([('email', '=', data.get('work_email')),limit=1])

if address:
address_id = address.id
else:
address_id = request.env['res.partner'].sudo().create({
'name': data.get('name'),
'email': data.get('email'),
}).id



employee = request.env['hr.employee'].sudo().search([('work_email', '=', data.get('work_email'))])
if employee:
return {'message': 'already exists'}
else:
id = request.env['hr.employee'].sudo().create({
'name': data.get('name'),
'work_email': data.get('email'),
'address_home_id' : address_id,
}).id
return {'message': 'Employee {} created'.format(id)}
Avatar
Discard