I'm trying to implement a boat company module.
I have various boats, and each boat has model options, like engine names, etc.
In my sale.order module, I have extended the module to be able to enter boat name
In the Order Lines page of that form, I have an option to select the model options relevant to that boat name. And I'm trying to implement a search function that refines the results of that drop down to only the models of that particular boat's id we just selected above.
I wrote the function but it's not working for some reason. Can you please help me out?
Here are the relevant files:
XML:
<record id="sale_view_order_form" model="ir.ui.view">
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="boatmodel_id"/>
</field>
<xpath expr="//field[@name='product_id']" position="attributes">
<attribute name="context">
{'boatmodel_id': parent.boatmodel_id}
</attribute>
</xpath>
</field>
</record>
PY:
class product_template(osv.osv):
_inherit="product.template"
_columns={
'boatlength':fields.char("Boat Length", size=18),
'fuelcapacity':fields.char("Fuel Capacity", size=18),
'modeloptions_id':fields.many2one("product.category","Model Options"),
}
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
print("Hii Search has started")
if context is None:
print("Heloo context was none")
context={}
if context.get('boatmodel_id'):
print("CONTEXT========")
print context['boatmodel_id']
#code to limit by category assigned to the boat model
productobj=self.pool.get('product.template').read(cr, uid, context['boatmodel_id'],['modeloptions_id'])
args=[('categ_id','=',productobj['modeloptions_id'][0])] + args
return super(product_template, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
class sale_order(osv.osv):
_inherit="sale.order"
_columns={
'boatmodel_id':fields.many2one("product.template","Boat Model", domain=[('categ_id.name','=','Boat Models')]),
}
Note:As you may have noticed, I have written print statements to test the outputs of the search function on the console. But nothing ever gets printed. I guess the search function never gets called. I'm using Odoo 8
Its obvious that your search method will not be called as you are calling and checking method of product_template and you have updated product_id's context which is of product_product. so when making change in product_product will not call search method of product_template.
Thanks for pointing it out. Can you suggest a way how I can keep things within product_template and implement the same functionality? Is there anything else I can use instead of product_id ?
But why are you changing template. According to me you are going wrong. instead you need to add this fields to product_product, Any ways its up requirements, But if you maintain though product_product it would be easy and well maintained.
Ok. I will try rewriting the code in product_product :)