使用Python API创建Odoo 11的sales.order遇问题求助
Hey Leonardo, sorry to hear you've been stuck on this for over three hours—Odoo's ORM can be tricky when you're first getting the hang of it! Since you mentioned you have a minimal example code and an error traceback, let's break down the most common pitfalls that cause sales order creation failures in Odoo 11, and then we can zero in on your specific case once you share the details:
Common Issues to Check First
- Missing Required Fields: Odoo 11's
sale.ordermodel has several mandatory fields that you might be overlooking. The big ones are:partner_id: You need to pass a validres.partnerrecord ID here (this is the customer linked to the order)pricelist_id: A valid product pricelist ID—usually linked to the customer or your system's default- If you're adding order lines (
order_line), each line dictionary needsproduct_id,product_uom_qty, andproduct_uom(unit of measure ID)
- Incorrect ORM Syntax: When creating records, make sure you're using the correct Odoo 11 method. The standard way is
env['sale.order'].create(vals)wherevalsis your dictionary of field-value pairs. Avoid outdated syntax or improper environment initialization here. - Permission Problems: If your user account doesn't have the necessary access rights to create sales orders, you'll hit an
AccessError. Double-check that your user is assigned to the "Sales Manager" or "Salesperson" security group. - Standalone Script Environment Issues: If you're running this code outside of Odoo (like a standalone Python script), you need to properly initialize the Odoo environment: load the config file, connect to the database, and set up the user context correctly. Skipping any of these steps will lead to errors.
Let's Get Specific!
Could you share your actual minimal code snippet (replace the [代码内容] placeholder) and the full error message you're seeing (from [报错信息])? For example, a MissingError would tell us exactly which required field you're missing, while an AttributeError might point to a typo in your field names.
Once you share those details, we can troubleshoot exactly what's going wrong with your implementation.
内容的提问来源于stack exchange,提问作者Leonardo Ramé

