OpenERP field attributes - invisible attribute in tree view - attributes

Considering the following objects and a corresponding view:
class first_object(osv.osv):
_name = "first.object"
_columns = {
'id': fields.integer ('First ID'),
'flag': fields.boolean ('Flag'),
'second_object_id': fields.one2many('second.object','first_object_id')
}
class second_object(osv.osv):
_name = "second.object"
_columns = {
'id': fields.integer ('Second ID'),
'first_object_id': fields.many2one('first.object','random field'),
'field_x': fields.float('x',size=128),
'field_y': fields.float('y',size=128),
}
<record model="ir.ui.view" id="first_object_view_id">
<field name="name">Frist Object</field>
<field name="model">first.object</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form>
<notebook>
<page>
<field name="id"></field>
<field name="flag"></field>
<field name="second_object_id">
<tree editable="top">
<field name="field_x" attrs="{'invisible':[('flag','=',True)]}"/>
<field name="field_y"/>
</tree>
<form>
<field name="field_x"/>
<field name="field_y"/>
</form>
</field>
</page>
</notebook>
</form>
</field>
</record>
Notice the attrs I have now for the field of the second object named field_x in the tree which is based on the field of the first object named flag.
First of all, the attribute in this case is ignored completely. I dont know why it wont work. Second, assuming this can't work and the attributes MUST refer to local fields, the invisible attribute does not work for the tree view, just the form view. However, if you set a simple invisible="1" in the tree it would work just fine (I cant rely on that, I need the rule I provide with attributes). Any ideas?
EDIT:
The problem seems to be making fields invisible via attributes (and not invisible="1") in the TREE view. It works fine in the form. If this can be done it would solve my problem.
EDIT 2:
I tried with separated view definitions and local fields instead of many2one and one2many to no avail. However, I managed to somehow achieve this with invisible="context.get('xxx',True/False)". The problem is once the condition is matched, it remains invisible even after creating a new record where the condition is not matched.

please look in stock_move_tree from stock.move
<field name="prodlot_id" groups="base.group_extended"/>
<button name="%(track_line)d" string="Split in production lots" type="action"
icon="terp-stock_effects-object-colorize" attrs="{'invisible': [('prodlot_id','<>',False)]}"
states="draft,waiting,confirmed,assigned,done"
groups="base.group_extended"/>
<field groups="base.group_extended" name="tracking_id"/>
<button name="setlast_tracking" string="Put in current pack" type="object"
groups="base.group_extended"
icon="terp-stock_effects-object-colorize" attrs="{'invisible': [('tracking_id','<>',False)]}"
states="draft,assigned,confirmed,done"/>
Is the same solution, but for button, not for regular field. And yes, remove field but show empty column.

it seems trying to set an conditional invisible attribute will not affect the true view. only invisible="1". which makes sense since i can't imagine a tree view with some invisible field of which the entire column itself is not invisible.

add a related field to flag in second.oject
class second_object(osv.osv):
_name = "second.object"
_columns = {
'id': fields.integer ('Second ID'),
'flag': fields.related('first_object_id', 'flag', type='boolean', relation='first.object', string='Flag'),
'first_object_id': fields.many2one('first.object','random field'),
'field_x': fields.float('x',size=128),
'field_y': fields.float('y',size=128),
}
an then add field flag in your view as invisible and attrs:
<record model="ir.ui.view" id="first_object_view_id">
<field name="name">Frist Object</field>
<field name="model">first.object</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form>
<notebook>
<page>
<field name="id"></field>
<field name="flag"></field>
<field name="second_object_id">
<tree editable="top">
<field name="flag" invisible="1"/>
<field name="field_x" attrs="{'invisible':[('flag','=',True)]}"/>
<field name="field_y"/>
</tree>
<form>
<field name="field_x"/>
<field name="field_y"/>
</form>
</field>
</page>
</notebook>
</form>
</field>

You have take 1 extra new field (i.e boolean type field) in Object2.
and create onchnage on "flag" field of object1.
in that onchnage you set-reset the value of this new field according to value of Flag field.
and put attrs on this new_field instead of Flag.
Hope This will help you

Please define the view for the model 'second.object' seperately. The same example is in stock_partial_picking.py file inside wizard folder in stock module. please check that. you may need to define a field as user user1888049 told in his answer

Related

reading a field value from another model odoo13

what i'm trying to do here is creating a new field dimension that gets value from a field in the [stock.move] model this approach doesn't work i don't know why and it's not getting any errors from the python file the only error from the xpath tag at the xml file
this is models.py file
xx_Custom_Invoice(models.Model):
_inherit = 'account.move'
dimension = fields.Char(String="XxX", compute='get_data')
def get_data(self):
stock_move = self.env['stock.move']
for move in self:
picking = stock_move.picking_id
if move.id != picking.account_move_ids:
continue
for line in move:
line.update({
'dimension': line.dimension,
})
here is the views.xml
<record id="xx_account_move_view_inherit" model="ir.ui.view" >
<field name="name">account.move.view.inherit</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="/form/sheet/notebook/page/field[#name='invoice_line_ids']/tree/field[#name='product_id']" position="after">
<field name="dimension"/>
</xpath>
</field>
</record>
There is an issue here:
stock_move = self.env['stock.move']
for move in self:
stock_move here is an empty recordset, so no iteration will happen in the for loop.
You probably to use search() to fetch the record you want to operate on:
stock_move = self.env['stock.move'].search(<DOMAIN>)
for move in self:

Compute boolean field with store=True is not search in Odoo11

For below code my boolean field not domain(searched) in database. It's work when i make this method depends. But i have above 1000 records so how to domain in action for all record simultaneously?
**Python Code:**
#api.multi
def _compute_opportunity_count111(self):
value = {}
for rec in self:
operator = 'child_of' if rec.is_company else '=' # the opportunity count should counts the opportunities of this company and all its contacts
won_list = rec.env['crm.lead'].search(
[('partner_id', operator, rec.id), ('stage_id.probability', '=', 100)]).ids
if won_list:
rec.won_customer = True
value.update(won_customer=rec.won_customer)
won_customer = fields.Boolean(compute='_compute_opportunity_count111', store=True)
**XML Code:**
<record id="base.action_partner_form" model="ir.actions.act_window">
<field name="name">Customers</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.partner</field>
<field name="view_type">form</field>
<field name="view_mode">kanban,tree,form</field>
<field name="context">{"search_default_customer":1}</field>
<field name="domain">[('won_customer', '=', True)]</field>
<field name="search_view_id" ref="base.view_res_partner_filter"/>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to add a contact in your address book.
</p>
<p>
Odoo helps you easily track all activities related to
a customer: discussions, history of business opportunities,
documents, etc.
</p>
</field>
</record>
Thanks in advance
You should not use api.multi but api.depends -> this makes sure the method is called again when relevant fields changed.
Seems you depend on is_company -> #api.depends('is_company')
Also, from what I read your value dict is useless -> you can remove it

how to run the cron job in odoo

Created the cron job for fetching weather information on every 1 minute but it not work. Here, I attach the code (.py function).
#api.model
def weather_cron(self):
weather_location_ids =self.env['weather_location.weather_location'].search([])
for weather_location_id in weather_location_ids:
url_cron = weather_location_id.api_address + weather_location_id.name
json_data = requests.get(url_cron).json()
formatted_data = json_data['weather'][0]['main']
formatted_data1 = json_data['main']['temp']
formatted_data2 = json_data['main']['temp_min']
formatted_data3 = json_data['main']['temp_max']
formatted_data4 = json_data['main']['humidity']
self.env['current_weather.current_weather'].create({
'weather_id':weather_location_id.id,
'main':formatted_data,
'temp':formatted_data1,
'temp_min':formatted_data2,
'temp_max':formatted_data3,
'humidity':formatted_data4,
})
Cron Job (.xml file):
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data noupdate="1">
<record forcecreate="True" id="create_weather_info_cron" model="ir.cron">
<field name="name">Weather Information</field>
<field name="user_id" ref="base.user_root"/>
<field name="active" eval="False" />
<field name="interval_number">1</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
<field name="model" eval="'weather_location.weather_location'"/>
<field name="function" eval="'weather_cron'"/>
</record>
</data>
</odoo>
You made the cron job inactive. Since it is not active this will not trigger the function you wrote. Please change the value to active as True
<field name="active" eval="True"/>
All your field are correct add this two:
<field name="args" eval="'()'"/>
<!-- delay the call 2 minutes just to make sure but it's optional -->
<field name="nextcall" eval="(DateTime.now() + timedelta(minutes=2)).strftime('%Y-%m-%d 00:00:00')" />
Now if the code sill not working you need to make sure of it.
#1- check that your file is in the __openerp__.py or __manifest__.py
#2- if you don't know how to use debug mode in your IDE just use logging to see if Odoo calls your methodname
Hope this Helps you
One thing if you used noupdate="1" in your xml file odoo will not update the record that it's inserted in first time no matter what you change in the code this will not effect the recrod in database.
just change the id and delete the ir.cron record manually from setting menu
EDITS:
every model with "model.name" have an xml_id like this model_model_name
when you see mode_id remember to prefix the name with _model
<field name="model_id" ref="weather_location.model_weather_location"/>
and they are in the same module just put ref="model_weather_location"
But for ir.cron just give the name of the model because it's a Char field not many2one:
<field name="model" eval="'weathe.location'"/>

Odoo rule One2Many relation Domain in one side. Different models

Im trying to make a rule that validates activity owners, are in the tutors pupil list. The problem is fields are in different model apart from the One2Many relation. Both classes here:
Activity:
class Activity(models.Model):
_name = "proyectosge.activity"
owner = fields.Many2one('res.users', string="Pupil",default=lambda self:
self.env.user,readonly=True)
Usuario:
class Usuario(models.Model):
_inherit = 'res.users'
tutor = fields.Many2one('res.users',string="Tutor")
pupils = fields.One2many('res.users','tutor',string = "Pupils")
And the rule I tried but obviusly not working cause they arent even on the same model:
<!--Tutor only view his pupil activities-->
<record model="ir.rule" id="activities_tutor_rule">
<field name="name">Tutor only see his pupil activities</field>
<field name="model_id" ref="model_proyectosge_activity"/>
<field name="groups" eval="[(4, ref('group_tutor'))]"/>
<field name="domain_force">[('owner','=',pupils)]</field>
<field name="perm_read" eval="True"/>
<field name="perm_create" eval="False"/>
<field name="perm_write" eval="False"/>
<field name="perm_unlink" eval="False"/>
</record>
Try this out if works. I am not 100% sure about this. Let me know if it works. Thanks
<field name="domain_force">[('owner','=',pupils.owner)]</field>

odoo[v8] rules for groups

I have a boolean field 'classified' on sale order and my idea was that only users who are in the group that I created 'Classified quotations' can see records on tree view in which classified is true . I created two rules and I have no idea why it doesn't work. Here is the code:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="sale_order_rule_group_classified_quotations" model="ir.rule">
<field name="name">sale_order_rule_group_classified_quotations</field>
<field name="model_id" search="[('model','=','sale.order')]" model="ir.model"/>
<field name="groups" eval="[(4,ref('group_classified_quotations'))]"/>
<field name="domain_force">['|',('classified','=',True),('classified','=',False)]</field>
</record>
<record id="sale_order_rule_no_group" model="ir.rule">
<field name="name">sale_order_rule_no_group</field>
<field name="model_id" search="[('model','=','sale.order')]" model="ir.model"/>
<field name="groups" eval="[(4,ref('base.group_user'))]"/>
<field name="domain_force">[('classified','=',False)]</field>
</record>
</data>
</openerp>
What am I doing wrong?
You didn't tell how it does not work (or how it behaves now). Also If you want to allow to see all records on rule, you should use this instead on domain_force: [(1,'=',1)], this means all records and you don't need to make True or False checking. Try if that works.
I've recently done something similar but done so in the .py file.
You can do something like this:
def write(self, cr, user, ids, vals, context=None):
if vals.get('classified'):
group_id = self.pool.get('ir.model.data').get_object_reference(cr, 1, 'your_model', 'your_group')
user = self.pool.get('res.users').browse(cr, user, user)
if group_id not in user.groups_id:
raise osv.except_osv(_('Error'), _("Only x user can adjust this field"))
return super(your_model, self).write(cr, user, ids, vals, context=context)

Resources