I am trying to generate different selection choices depending on other field in the Model. I am working in Odoo 8.
Below is the code :
My Model: (test.py)
type = fields.Selection(selection='_get_selection', string='Type')
#api.onchange('role_name')
def _get_selection(self):
choise = []
if self.role_name == 'primary':
choise.append(('unit','Unit Case'))
if self.role_name == 'reserve':
choise.append(('res','Reserve Case'))
return choise
The View: (template.xml)
<field name="type" widget="selection"/>
But i don't see any values in Selection Field.
Please Help.
Thanks,
This is an example from 'account.invoice' in Odoo 8:
#api.model
def _get_reference_type(self):
return [('none', _('Free Reference'))]
reference_type = fields.Selection('_get_reference_type', string='Payment Reference',
required=True, readonly=True, states={'draft': [('readonly', False)]},
default='none')
This should help you in resolving the issue.
#api.model
def _get_selection(self):
#DO SOMETHING
return choise
type = fields.Selection(selection=_get_selection, string='Type')
Related
I want to get id from url when i click the button.
This is URL, id is 69:
http://localhost:8069/web#id=69&cids=1&menu_id=385&action=507&model=inventory.menu&view_type=form
I need to get this id in many2one field.
This is my wizard py file:
from odoo import api,fields,models
class ReduceInventoryWizard(models.TransientModel):
_name = "reduce.inventory.wizard"
_description = "Reduce Inventory Wizard"
inventory_ids = fields.Many2one('inventory.menu', string="Ürün Referans No: ", default=lambda self: self.env['inventory.menu'].search([('id', '=', 69)], limit=1))
As you can see, ('id', '=', 69) this is running but just one product. I want the information of that product to come automatically when I click the button in which product.
I tried this one: ('id', '=', self.id). But is not working.
In this situation there should be active_id or better active_ids in Odoo's context.
So you just can set the default parameter to use a default method, which will either return a value or an empty recordset:
def default_my_many2one_field_id(self):
active_ids = self.env.context.get("active_ids")
if active_ids:
return self.env["another.model"].browse(active_ids[0])
return self.env["another.model"]
my_many2one_field_id = fields.Many2one(
comodel_name="another.model", string="My M2o Field",
default=default_my_many2one_field_id)
I have odoo 8. I want to count the attachment from ir_attachment and and show it in stock.production.lot. Here is my .py
class stock_production_lot(models.Model):
_inherit='stock.production.lot'
#api.multi
def get_attachment_info(self):
for lot in self:
so_line_ids = self.env['ir.attachment'].search([('res_id','=',lot.id)])
for pick in so_line_ids:
pick.count_attachment = 0
if pick.datas_fname:
pick.count_attachment = len(pick.datas_fname)
count_attachment = fields.Float(string='Total Attachment', compute='get_attachment_info')
and this the view
<field name="count_attachment" />
Thanks
It's difficult to answer, because the information in your question are a bit poor. But let me try to answer it in a general way with a general example how i would do it.
Let's say you want a count of all attachments of model stock.picking (Delivery Nots, Slips, Receipts, and so on).
So you need to add a computed field, which could be stored, but it's difficult to trigger a recalculation of this field, because attachments are related to records indirectly (no real foreign keys used in database).
class StockPicking(models.Model):
_inherit = 'stock.picking'
attachment_count = fields.Integer(compute="_compute_attachment_count")
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking.attachment_count = self.search_count(domain)
And also add the new field to a view of model stock.picking.
Now let's pretend that you don't only want the attachments of the pickings but also of their move lines, too.
Just "count" them and add that count to the previous one:
#api.multi
def _compute_attachment_count(self):
for picking in self:
domain = [('res_id', '=', picking.id),
('res_model', '=', self._name)]
picking_count = self.search_count(domain)
if picking.move_lines:
domain_move = [('res_id', 'in', picking.move_lines.ids),
('res_model', '=', picking.move_lines._name)]
picking_count += picking.move_lines.search_count(domain_move)
picking.attachment_count = picking_count
Thank you for the respone. I have got the answer
#api.one
def count_attachments(self):
obj_attachment = self.env['ir.attachment']
for record in self:
record.count_attachment = 0
attachment_ids = obj_attachment.search([('res_model','=',self._name),('res_id','=',record.id)])
if attachment_ids:
record.count_attachment = len(attachment_ids)
sorry my english. I need a help with a issue I have. I defined a funtion to bring values attributes products however, when I run it, the result is
ValueError: Expected singleton: product.template.attribute.value(9, 25)
will somebody guide me to solve it? I dont know how to go on
class MRPSalesProduc(models.Model):
_inherit = 'mrp.production'
product_short_name = fields.Char('Productos')
#api.model
def create(self, vals):
product_short_name = self.env['sale.order'].search([('name', '=', vals[
'origin'])]).order_line.product_id.product_template_attribute_value.attribute_id
vals['product_short_name'] = product_short_nam
rec = super(MRPSalesProduc, self).create(vals)
return rec
You can use a related many2many field to get product attributes
Example:
class MRPSalesProduct(models.Model):
_inherit = 'mrp.production'
product_template_attribute_value_ids = fields.Many2many(related='product_id.product_template_attribute_value_ids')
Then use the man2many_tags widget to show the product attributes as tags:
<field name="product_template_attribute_value_ids" widget="many2many_tags"/>
Example (Product name):
class MRPProductsName(models.Model):
_inherit = 'mrp.production'
products_name = fields.Char(related="product_id.product_tmpl_id.name", string='Productos')
You have tried to search sale order and access all sale order lines.
If you pick first line from the sale order line, then it will be work as per your expectation. For example,
sale_order = self.env['sale.order'].search([('name', '=', vals['origin'])])
product_short_name = sale_order and sale_order.order_line[0].product_id.product_template_attribute_value.attribute_id
if product_short_name:
vals['product_short_name'] = product_short_nam
You can reference my blog https://odedrabhavesh.blogspot.com/2017/02/valueerror-expected-singleton-in-odoo.html for more about "ValueError: Expected singleton"
I am developing a custom report in ODOO10 CE and I have a problem naming the generated PDF. It always names the "string" field in the report definition. It ignores the print_report_name field.
Here is the report definition:
<report
id="action_report_as"
model="report_as"
string="Report AS_EE"
report_type="qweb-pdf"
name="report_as_ee.report_as"
paperformat="report.vertical_1"
/>
<record
id="action_report_as"
model="ir.actions.report.xml">
<field name="print_report_name">mycorrectreportname.pdf</field>
</record>
And here is the .py:
class ReportAs(models.AbstractModel):
_name = 'report.report_as_ee.report_as'
#api.model
def render_html(self, docids, data=None):
self.model = self.env.context.get('active_model')
docs = self.env[self.model].browse(self.env.context.get('active_id'))
as_records = []
#
# code that puts records in as_records array
#
docargs = {
'doc_ids': self.ids,
'doc_model': self.model,
'docs': docs,
'time': time,
'as': as_records
}
return self.env['report'].render('report_as_ee.report_as', docargs)
Maybe I need to put the name from the .py? How?
Thanks.
you can do it like this, go to Settings -> Technical ->reports ->reports, select the report of the sales,
Example, Report Quotation / Order. Then open the report and add
"((object.name or '').replace('/','')+'.pdf')" this to field Printed Report Name.
Check with Quotation/Sale Order Print.
Thanks
I need to filter products in purchase order line based on value in parent table (purchase.order).
Purchase_order.py
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
product_variety_type = fields.Selection([('raw_material','Raw Material'),('stationary','Stationary')],string='Product Variety')
I already defined a domain filter in purchase.order.line.py, need to add one more condition ('variety','=',self._context.get('variety')
class PurchaseOrderLines(models.Model):
_inherit = 'purchase.order.line'
#api.onchange('product_id')
#api.depends('order_id')
def onchange_product_id2(self):
product_variety_type = self._context.get('variety') // Value is None
result = super(PurchaseOrderLines,self).onchange_product_id()
supplier_infos = self.env['product.supplierinfo'].search([('name', '=', self.partner_id.id)])
product_ids = self.env['product.product']
if not supplier_infos:
product_ids = product_ids.search([])
for supplier_info in supplier_infos:
product_ids += supplier_info.product_tmpl_id.product_variant_ids
result.update({'domain': {'product_id': [('id', 'in', product_ids.ids)]}})
return result
I tried to pass a context value from parent view,but no luck.
*.xml
<field name="partner_id" position="after">
<field name="product_variety_type" required="1" context="
{'variety':product_variety_type}"/>
</field>
How can i do this?
You don't have to use the context. Just use self.order_id to get the value.
#api.onchange('product_id')
def onchange_product_id2(self):
product_variety_type = self.order_id.product_variety_type
# and so on