You need to declare the BoM before you create the Manufacturing Order. Have you tried this?
Edit:
The products must be declared before as well.
What we do is:
a) Create parent product (the one containing the BOM) [product.template].
b) Create child products (the ones inside the BOM) [product.template].
c) Create the BOM [mrp.bom].
d) Create the BOM lines [mrp.bom.line].
Hope that clarifies something.
And about the on change part. You should call it like this:
@api.onchange('partner_id')
def your_func():
'partner_id' is just an example, you need to find your object. I think you need 'product_tmpl_id' from mrp.bom.