Show records on TreeView that are related to Active User - odoo

I'm Trying to show records based on the active user,
My task_submission.py
class TaskSubmission(models.Model):
_name = 'training_program_management.task_submission'
solution_description = fields.Html(string="Task submission's Description", required=True)
partner_id = fields.Many2one('res.partner', string='trainee: ',
default=lambda self: self.env.user.partner_id.id, readonly=True)
task_id = fields.Many2one('project.task', readonly=True, string="submitting Task :")
project_id = fields.Many2one(related="task_id.project_id", string="Practical work ", readonly=True)
In TreeView i need to show only task submissions with
Partner_id= current_user_id.partner_id
I tried this but it doesn't seem to be working:
<record model="ir.actions.server" id="mysub_task_action">
<field name="name">My Task submissions</field>
<field name='model_id' ref='training_program_management.task_submission'/>
<field name="state">code</field>
<field name="code">
action = {
'type': 'ir.actions.act_window',
'name': 'My task submissions',
'view_mode': 'tree',
'view_type': 'tree',
'view_id': env.ref("training_program_management.mysub_task_tree_view").id,
'context': '{ "tree_view_ref":"mysub_task_tree_view"}',
'res_model': 'training_program_management.task_submission',
'views': [(env.ref('training_program_management.mysub_task_tree_view').id, 'tree')],
'domain' : '[("partner_id", "=","user.id.id_partner.id")]',
}
</field>
</record>
Does anyone have an idea of how to do it ?

It doesn't work because you mustn't use quotes around user.id.id_partner.id in your domain : user is actually a python object you can use in domains.
'domain' : '[("partner_id", "=", user.partner_id.id)]',
Should work.

Related

Odoo search_default_product_id by passing string inside context

I have a wizard that let the users to open a tree of recors with field product_id.
This model view has a searchview with field product_id, like that:
<record id="view_purchase_product_control_panel_line_search" model="ir.ui.view" >
<field name="name">view.purchase.product.control.panel.line.search</field>
<field name="model">purchase.product.control.panel.line</field>
<field name="arch" type="xml">
<search>
<field name="product_id"/>
<field name="product_name" string="Nome Prodotto"/>
<field name="numerical_code"/>
</search>
</field>
</record>
From the Wizard i let the user to input a product name so i can open the tree view with Products filter set with the given string.
Like that:
def method(self):
# other stuffs
return {
'name': view_name,
'view_type': 'form',
'view_mode': 'form,tree',
'res_model': 'purchase.product.control.panel.line',
'search_view_id': search_id,
'domain': [('id', 'in', line_ids)],
'context': ctx,
'view_id': False,
'views': [(tree_id, 'tree'), (form_id, 'form')],
'type': 'ir.actions.act_window',
}
Where ctx is set by this method:
#api.multi
def get_product_tree_context_searches(self):
self.ensure_one()
ctx = dict()
if self.name:
ctx['search_default_product_id'] = self.name
if self.from_numerical_code:
ctx['search_default_numerical_code'] = self.from_numerical_code
return ctx
The problem came up when i type the string name of product in try to launch the tree view.
The log say that can pass a string to product_id searchview field.
But if i use the Odoo searchview i can type a product name and the search work perfectly.
At this point i make a related field product_id.name so i can search the products by this field (present in the searchview)
Is there some way to passing string with ctx['search_default_product_id'] = self.name?
Or the only way is to make a related, string field as i made?

Odoo 10 project version

I'm trying to port Project issue module from Odoo 8 to Odoo 10 to add version field to projects.
project_version.py :
class project_version(models.Model):
_inherit = 'project.project'
_name = "project.version"
_order = "name desc"
_columns = {
'name': fields.char('Version Number', required=True),
'active': fields.boolean('Active', required=False),
}
_defaults = {
'active': 1,
}
When I try to install it, Odoo say
Model not found: project.project.version
Error context:
View `project_version list`
[view_id: 750, xml_id: n/a, model: project.project.version, parent_id: n/a]
None" while parsing file:///c:/Program%20Files%20(x86)/Odoo%2010.0/server/custom/project_task_version/views/views.xml:9, near
<record model="ir.ui.view" id="project_version.list">
<field name="name">project_version list</field>
<field name="model">project.version</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="active"/>
</tree>
</field>
</record>
This error means, that Odoo can't find you model. As I see, you set name "project.version" to you model, but Odoo searching for "project.project.version". So, just try to change name of model from "project.version" to "project.project.version".
Next:
This type of declaration of model now is not supported by Odoo 10:
_columns = {
'name': fields.char('Version Number', required=True),
'active': fields.boolean('Active', required=False), }
Try to look at this documentation - https://www.odoo.com/documentation/10.0/howtos/backend.html#model-fields
You must replace the declaration _columns and _default by the following: these being used by you is from old API syntax.
The syntax about your field attributes should be like this:
name = fields.Char('Version Number', required=True)
active = fields.Boolean('Active', required=True, default=True)

How to create a manual view in odoo?

The below view is not created. What is the issue?
Method:
def daily_flash_report_tree(self, cr, uid, ids, context=None):
sql = """
CREATE OR REPLACE VIEW report_view AS (
SELECT
id,name,job
from
sales_summary limit 10
)
"""
cr.execute(sql)
return {
'name': "Daily Flash Report",
'view_type': 'form',
'view_mode': 'tree',
'res_model': 'daliy.flash.report',
'type': 'ir.actions.act_window',
'context': {"search_default_group_period": 1},
}
Object:
class daily_flah_report_new(osv.osv):
_name = "daliy.flash.report"
_auto = False
_columns = {
'name': fields.char('Name'),
'job': fields.char('Job'),
}
View:
<record id="drill_flash_report_flash" model="ir.ui.view">
<field name="name">Report</field>
<field name="model">daliy.flash.report</field>
<field name="arch" type="xml">
<tree>
<field name="name" />
<field name="job" />
</tree>
</field>
</record>
<record id="drill_flash_report_action" model="ir.actions.act_window">
<field name="name">Net Revenue</field>
<field name="res_model">daliy.flash.report</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">tree</field>
<field name="context">{"search_default_group_period": 1}</field>
</record>
please notice that you created view named "report_view"
CREATE OR REPLACE VIEW report_view AS (
SELECT
id,name,job
from
sales_summary limit 10
)
Because your object is daliy.flash.report so your default table of your object is daliy_flash_report
_name = "daliy.flash.report"
_auto = False
They are different, you should make sure name of table of object is same as name of the view.
Solution: choose 1 or 2.
You should create view named daliy_flash_report by below comment
CREATE OR REPLACE VIEW daliy_flash_report AS (
SELECT
id,name,job
from
sales_summary limit 10
)
Use attribute _table in your object to indicate table name.
class daily_flah_report_new(osv.osv):
_name = "daliy.flash.report"
_auto = False
_table = "report_view"
Good luck

Odoo 10: show field of model "sale.order" in form view of "account.invoice"

I usually create a new Database Structure Field by using the Debugging Mode, then Edit FormView and writing e.g. <field name="x_delivery_date"/>. Also I can show it later on the printed report like this:
<div name="x_delivery_date" t-if="doc.x_delivery_date">
<strong>Delivery Date:</strong>
<p t-field="doc.x_delivery_date"/>
</div>
But how do I display a field (commitment_date), which is available in the model (sale.order) in another models formview (account.invoice)? I guess that I have to use object relations or related field, but I don't know how. I hope somebody can help me. Many thanks in advance.
You can use related fields for that. You have to add two fields to account.invoice to do it.
class AccountInvoice(models.Model):
_inherit = "account.invoice"
order_id = fields.Many2one('sale.order', 'Related_order')
commitment_date = fields.Date(related='order_id.commitment_date')
Then you can use the commitment_date fields in account.invoice forms. The value of the field in sale.order will be reflected on the form right away. But be aware that changing the value of that field will change the value of that field on the sale.order as well.
EDIT
For reports just use the field like it is a regular field of account.invoice (so doc.commitment_date)
First you need to add a many2one field in account.invoice
class account_invoice(osv.osv):
_inherit = "account.invoice"
_columns = {
'source_id':fields.many2one('sale.order','Source')
}
Then inherit the _prepare_invoice function in sale_order. In this function you are going to pass the sale order id as source id to the account.invoice
class sale_order(osv.osv):
_inherit = "sale.order"
def _prepare_invoice(self, cr, uid, order, lines, context=None):
if context is None:
context = {}
journal_id = self.pool['account.invoice'].default_get(cr, uid, ['journal_id'], context=context)['journal_id']
if not journal_id:
raise osv.except_osv(_('Error!'),
_('Please define sales journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
invoice_vals = {
'name': order.client_order_ref or '',
'origin': order.name,
'type': 'out_invoice',
#Sale order id as source_id
'source_id':order.id,
'reference': order.client_order_ref or order.name,
'account_id': order.partner_invoice_id.property_account_receivable.id,
'partner_id': order.partner_invoice_id.id,
'journal_id': journal_id,
'invoice_line': [(6, 0, lines)],
'currency_id': order.pricelist_id.currency_id.id,
'comment': order.note,
'payment_term': order.payment_term and order.payment_term.id or False,
'fiscal_position': order.fiscal_position.id or order.partner_invoice_id.property_account_position.id,
'date_invoice': context.get('date_invoice', False),
'company_id': order.company_id.id,
'user_id': order.user_id and order.user_id.id or False,
'section_id' : order.section_id.id
}
invoice_vals.update(self._inv_get(cr, uid, order, context=context))
return invoice_vals
Add this in View file
<record id="invoice_form" model="ir.ui.view">
<field name="name">account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='date_invoice']" position="after">
<field name="source_id"/>
</xpath>
</field>
</record>
Now add this in your report file
<div name="x_delivery_date" t-if="doc.x_delivery_date">
<strong>Delivery Date:</strong>
<p t-field="doc.x_delivery_date"/>
<p t-field="doc.source_id.commitment_date"/>
</div>

Autofill some fields Openerp

Hi I am new to OpenERP7/Odoo7 and not really getting how to do this.
I have a existing records in my custom product module and I want to retrieve that record by entering only to my prodcode fields and want to autofill some fields.
I have been reading a lot on the internet, Odoo forums, but cannot find the answer or I just don't understand it. I already have post with regards to my problem
So if is there someone could give me an answer on how to achieve this, or direct me to a easy to understand explanation.
That would be great.
Here is I have done already
PYTHON---
class test_product(osv.Model):
_name = "test.product"
def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):
return {
'value': {
'prodname': 'Plastic Ware',
'desc': 'Affordable Plastic Ware',
'price': 100.00,
}
}
_columns = {
'test_prodcode_id': fields.many2one('test.prodcode', 'Code'),
'prodname': fields.char('Name', size=32),
'desc': fields.char('Description', size=32),
'price': fields.float('Price',),
}
class test_prodcode(osv.Model):
_name = "test.prodcode"
_columns = {
'name': fields.char('Product Code', size=32),
}
XML----
<record id="test_product_form_view" model="ir.ui.view">
<field name="name">test.product.form.vew</field>
<field name="model">test.product</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Product" version="7.0">
<sheet>
<field name="test_prodcode_id" on_change="name_change(test_prodcode_id,prodname,desc,price)"/>
<field name="prodname"/>
<field name="desc"/>
<field name="price"/>
</sheet>
</form>
</field>
</record>
Yeah, I've had this problem too.
I've got the solution. Auto fill can be achieved by doing
_defaults = {}
, or, as in your example, by doing
class test_product(osv.Model):
_columns = {
'test_prodcode_id': fields.many2one('test.prodcode', 'Code'),
'prodname': fields.char('Name', size=32),
'desc': fields.char('Description', size=32),
'price': fields.float('Price',),
}
_defaults: {
'test_prodcode_id' : 'defaults_prodcode',
}
You can also assign a function that return the value of the defaults.
I have solution but it is not as exactly as you want but you can do like
this also.
You can use related for auto fill your other fields. Suppose you set
product_id it will auto fill product_name, desc and price regarding to
your product_id.
You can also see Example here :
https://stackoverflow.com/a/42453539/5161074