Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
679 Weergaven

I am adding a supplier for Lunch via the back-end (via a module ofcourse) because it auto-updates with the vendor. The whole auto-update shazam seems to work.


I have been struggling to find the correct code to get my module to create the supplier. The code that searches a specific vendor works. I also have code that creates catogories depending on the tags in the json data, and that all works. 
The vendors name is not in the data, has to be specified in this piece of code. For privacy reasons I just named it Vendor123.


I made 4 attempts that I will be sharing here (plz note, all of them do not work, almost all of them give error for Partner_id, so it seems that is the problem.). Plz help.

(Vendor123_Supid is a model I made to just inherit lunch.supplier, thats it.)

class LunchSupplier(models.Model):
_inherit = 'lunch.supplier'


Vendor123_Supid = fields.Text('Vendor123 Supid')


These are my 4 attempts.

supplier = self.env['lunch.supplier'].search([('name', '=', 'Vendor123')])
if not supplier:

supplier = self.env['lunch.supplier'].create({'name': 'Vendor123',
'partner_id': cron.ir_actions_server_id.id}) #1

supplier = {
'supplier_id': supplier_id.create('Vendor123'),
'supplier_name': 'Vendor123',
} #2

supplier = supplier.create({'Vendor123_Supid': params['partner_id'],
'name': 'Vendor123'}) #3

supplier = self.env['res.partner'].create({'id': 123, 'name': 'Vendor123'})
supplier_data = {
'name': 'Vendor123',
}
supplier_id = supplier.create(supplier_data)
print(supplier_id.id) #4
Avatar
Annuleer
Auteur Beste antwoord

Thank you Econ Odoo, that helped massively!

Tiny change I made: I ofcourse wanted it to have a id created by Odoo themselves. I just put in 123 as a placeholder. I forgot to say that.


So in the supplier_contact code it should probably direct Odoo to create an ID themselves for the supplier. This is probably sufficiantly managed in the res.partner code. I cannot find a def create in any Res_partner model that makes the ID, but in the Lunch_supplier model it states the following as part of the creating proces of a supplier:

self.env['ir.model.data'].sudo().create([{
'name': f'lunch_supplier_cron_sa_{cron.ir_actions_server_id.id}',
'module': 'lunch',
'res_id': cron.ir_actions_server_id.id,
'model': 'ir.actions.server',
# noupdate is set to true to avoid to delete record at module update
'noupdate': True,
} for cron in crons])
for vals, cron in zip(vals_list, crons):
vals['cron_id'] = cron.id

To me it seems that the

'res_id': cron.ir_actions_server_id.id,

part initiates Odoo to create an ID.

Which to me meant that if I changed my code to the following:

supplier = self.env['lunch.supplier'].search([('name', '=', 'Vendor123')])
if not supplier:
supplier_contact = self.env['res.partner'].create({'res_id': cron.ir_actions_server_id.id, 'name': 'Vendor123'})
supplier = self.env['lunch.supplier'].create({'name': 'Vendor123',
'partner_id': supplier_contact.id})

However, that gave me the following error:

NameError: name 'cron' is not defined

I am a beginnner, and yes I am creative, so I continued.

Since the id creation is taken care of in the res_partner model, I just skipped the ID part and only listed name as data to help Odoo create. That worked fantastically!

Final working code:

supplier = self.env['lunch.supplier'].search([('name', '=', 'Vendor123')])
if not supplier:
supplier_contact = self.env['res.partner'].create({'name': 'Vendor123'})
supplier = self.env['lunch.supplier'].create({'name': 'Vendor123',
'partner_id': supplier_contact.id})

Thanks for your help again Econ Odoo. I hope someone in the future might learn from my mistakes :)

Avatar
Annuleer
Beste antwoord

Partner id is the contact of the Vendor. If you're creating a new Vendor, you're supposed to create a new contact for it first and use that for the vendor creation.

Your code:

supplier_contact = self.env['res.partner'].create({'id': 123, 'name': 'Vendor123'})

This will have the new partner. Then 

 supplier = self.env['lunch.supplier'].create({'name': 'Vendor123',
'partner_id': supplier_contact.id})

 But reading your code, you seems to be super creative in using variables using just a single variable supplier for everything which may confuse python, Odoo and possibly anyone who are trying to help you here. Be less creative and use a different variable for different thing.

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
aug. 25
1549
1
aug. 25
503
0
jul. 25
657
0
jul. 25
1236
1
jun. 25
1198