How to get field value from another Many2many field? - python-2.7

I use odoo 10 and I created a custom module to plan trips. I create a view for the plans in which I will select the list of my travels. My problem now is how I can get the Start and Destination fields of each trip insert in this view knowing that the field that displays the list of trips is travel_ids = fields.Many2many ('tms.travel', copy = False, string = 'Travels'). I try a lot but no result. Any idea for help please ??
tms_travel_planning.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record id="view_tms_travel_planning_form" model="ir.ui.view">
<field name="name">view.tms.travel.planning.form</field>
<field name="model">tms.planning</field>
<field name="arch" type="xml">
<form string="Plannification des voyage">
<header>
<field name="state" statusbar_visible="draft,approved,confirmed" widget="statusbar"/>
</header>
<sheet>
<div class="oe_title">
<h1>
<label class="oe_inline" style="font-size:30px;" string="Plannification - " attrs="{'invisible':[('name','=', False)]}"/>
<field name="name" readonly="1"/>
</h1>
</div>
<group>
<group>
<field name="datetime"/>
<field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" name="operating_unit_id"/>
</group>
<group>
<field name="num_vehicule_dispo"/>
<field name="num_chauffeur_dispo"/>
</group>
</group>
<notebook colspan="1">
<page string="Les voyages à planifier">
<separator coslpan="4" string="Voyages"/>
<!--<field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" name="operating_unit_id"/>-->
<field attrs="{'readonly':[('state','in',('confirmed', 'cancel'))]}" colspan="4"
domain="[('state','not in',('cancel','closed'))]" name="travel_ids" nolabel="1"/>
<separator coslpan="4" string="Véhicules"/>
<field colspan="4" name="fleet_ids" nolabel="1"/>
<separator coslpan="4" string="Conducteurs"/>
<field colspan="4" name="employee_ids" nolabel="1"/>
</page>
</notebook>
</sheet>
</form>
</field>
</record>
</data>
</odoo>
tms_travel_planning.py
# -*- coding: utf-8 -*-
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class TmsTravelPlanning(models.Model):
_name = 'tms.planning'
name = fields.Char('Num du plannification')
operating_unit_id = fields.Many2one(
'operating.unit', string='Operating Unit', required=True)
id_planning=fields.Integer(string="Numéro du plannification")
datetime=fields.Date(string="Date")
num_vehicule_dispo=fields.Integer(string="Nombre de véhicule disponible")
num_chauffeur_dispo=fields.Integer(string="Nombre de chauffeur disponible")
tms
fleet_ids = fields.Many2many('fleet.vehicle', copy=False, string='Véhicules')
employee_ids = fields.Many2many('hr.employee', copy=False, string='Conducteurs')
state = fields.Selection([
('draft', 'Pending'),
('approved', 'Approved'),
('confirmed', 'Confirmed'),
('cancel', 'Cancelled')], readonly=True,
help="Gives the state of the Waybill.",
default='draft')
#api.model
def create(self, values):
planning = super(TmsTravelPlanning, self).create(values)
if not planning.operating_unit_id.planning_sequence_id:
raise ValidationError(_(
'You need to define the sequence for planning in base %s' %
planning.operating_unit_id.name
))
sequence = planning.operating_unit_id.planning_sequence_id
planning.name = sequence.next_by_id()
print(str(values['num_vehicule_dispo']))
print(str(values['num_chauffeur_dispo']))
return planning
tms_travel.xml
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_tms_travel_tree" model="ir.ui.view">
<field name="name">tms.travel.tree</field>
<field name="model">tms.travel</field>
<field name="priority">1</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="date"/>
<field name="departure_id"/>
<field name="arrival_id"/>
<field name="state"/>
<button groups="tms.group_traffic,tms.group_expenses" icon="fa-thumbs-up" name="action_progress" states="draft" string="Dispatch Travel" type="object"/>
<button groups="tms.group_traffic,tms.group_expenses" icon="fa-check-square" name="action_end" states="progress" string="End Travel" type="object"/>
</tree>
</field>
</record>
</odoo>

Ok, you are missing some information in your many2many relationship. here is what you need.
field_name = fields.Manmy2many('related.model', 'relational_table', 'current_model_id', 'related_model_id', string='other information')
and usually I put the inverse on the related model ex:
(On the hr.holidays model)
payslip_ids = fields.Many2many('hr.payslip', 'hr_payslip_holiday_rel', 'holiday_id', 'payslip_id', ...)
(On the hr.payslip model)
holiday_ids - fields.Many2many('hr.holidays', 'hr_payslip_holiday_rel', 'payslip_id', 'holiday_id', ...)
Then at some point you need to add one of the ids to the other model for ex:
holiday.payslip_ids |= current_payslip_id

Related

*Odoo 10* How to generate report to xls/xlsx file?

I like to generate a report to xls/xlsx file. I want to get all the product list on another Model to a TransientModel. ( I do my research but without someone explaining it to me I am not able to get the whole picture. )
aging_inventory_report.py
***aging_inventory_report.py***
from time import strftime, gmtime
from odoo import models, fields, api
from report_xlsx.report.report_xlsx import ReportXlsx
class AgingInventoryReport(models.TransientModel):
_name = 'aging.inventory.report'
_inherit = 'product.template'
_description = 'Aging of Inventory Report'
***from here, these are the codes that I get from searching over internet***
def print_xls_report(self, cr, uid, ids, context=None):
data = self.read(cr, uid, ids)[0]
return {'type': 'ir.actions.report.xml',
'report_name': 'dealer_stock_aging_report.report_name.xlsx',
'datas': data
}
class ClassABCD(ReportXlsx):
def generate_xlsx_report(self, workbook, data, lines):
current_date = strftime("%Y-%m-%d", gmtime())
logged_users = self.env['res.users'].search([('id', '=', data['create_uid'][0])])
sheet = workbook.add_worksheet()
# add the rest of the report code here
ClassABCD('report.dealer_stock_aging_report.report_name.xlsx', 'product.template')
*** I don't under stand the code but it successfully integrate into my model.***
XML
aging_inventory_report.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_aging_inventory_report" model="ir.ui.view">
<field name="name">Aging of Inventory</field>
<field name="model">aging.inventory.report</field>
<field name="arch" type="xml">
<form string="Aging Inventory">
<group>
<p>
Retrieve list of Aging Inventory.
</p>
</group>
<footer>
<button name="print_xls_report" string="Save as XLS" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel" />
</footer>
</form>
</field>
</record>
</odoo>
view_aging_inventory_report.xml
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="action_view_aging_inventory_report" model="ir.actions.act_window">
<field name="name">Retrieve Aging Inventory Report</field>
<field name="res_model">aging.inventory.report</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_aging_inventory_report"/>
<field name="target">new</field>
</record>
<menuitem id="action_menu_aging_inventory_report"
name="Aging Inventory"
action="action_view_aging_inventory_report"
parent="dealer_inventory_cost.inventory_cost"
sequence="2"/>
</odoo>
Hoping someone could help me on this. Thank you. Id something is not clear please let me know.

view details in tree view from original model (Many2one) - Odoo 9

I have simple database which has 3 models as follows
1- camps (to store Camps master data)
2- players (to store players master data)
3- players_camps (to store camps each player has attended)
the code for all models as follows:
class Camps(Model):
_name = 'camps'
name = Char('Name')
organizer = Char()
date_from = Date('From date')
date_to = Date('To date')
place = Char()
supervisor = Char()
notes = Text()
class players(Model):
_name = 'player'
name = Char()
camps = One2many('player_camps', 'player')
class player_camps(Model):
_name = 'player_camps'
camps = Many2one('camps', on_delete='CASCADE', on_update='CASCADE')
organizer = Many2one('camps', on_delete='CASCADE', on_update='CASCADE')
place = Many2one('camps', on_delete='CASCADE', on_update='CASCADE')
date_from = Many2one('camps', on_delete='CASCADE', on_update='CASCADE')
date_to = Many2one('camps', on_delete='CASCADE', on_update='CASCADE')
result = Integer(default=0)
when I display players camps (using One2many widget in the view ) I can see only the camp name but the other data
like organizer or place (which are stored in master model of camps) not shown
how can I show this data in the tree view of the players camps view ?
Edit: here is view file for players
Note: I have removed some fields from players view and its model to make the code shorter
<openerp>
<data>
<!--Form view -->
<record id="Player_form_view" model="ir.ui.view">
<field name="model">player</field>
<field name="arch" type="xml">
<form>
<notebook>
<page name="basic_data" string="Basic Data">
<group>
<field name="name"/>
</group>
</page>
<page name="camps" string="Camps">
<field name="camps">
<tree>
<field name="camp"/>
<field name="instructor"/>
<field name="organizer"/>
<field name="place"/>
<field name="date_from"/>
<field name="date_to"/>
<field name="result"/>
<field name="notes"/>
</tree>
</field>
</page>
</notebook>
</form>
</field>
</record>
</data>
</openerp>
Please remove widget from one2many list and in <tree> tag declared field as many as you want to show.
Try with following code:
<openerp>
<data>
<!--Form view -->
<record id="player_form_view" model="ir.ui.view">
<field name="name">player.form.view</field>
<field name="model">spogaze.player</field>
<field name="arch" type="xml">
<form>
<notebook>
<page name="basic_data" string="Basic Data">
<group>
<field name="name"/>
</group>
</page>
<page name="exams" string="Exams">
<field name="exams">
<tree string="Exam" editable="bottom">
<field name="camp"/>
<field name="instructor"/>
<field name="organizer"/>
<field name="place"/>
<field name="date_from"/>
<field name="date_to"/>
<field name="result"/>
<field name="notes"/>
</tree>
</field>
</page>
</notebook>
</form>
</field>
</record>
</data>
</openerp>

How to get many2one for same calendar instance in odoo?

The following code is calendar.event inherited module so here I will store some inspection and all. Here relation is working fine.
from openerp.osv import fields, osv
from openerp import api
class calendar_event(osv.osv):
_inherit = "calendar.event"
STATE_SELECTION = [
('1', 'Corrective'),
('2', 'Preventive'),
('3', 'Predictive'),
]
SD_SELECTION = [
('Shut Down Required', 'Shut Down Required'),
('Shut Down Not Required', 'Shut Down Not Required'),
]
_columns = {
'number' : fields.char('Calendar Inspection ID',readonly=1),
'tags' : fields.selection(STATE_SELECTION, 'Tags',),
'user_id': fields.many2one('res.users','res_users_rel','Assigned', track_visibility='onchange',),
'sht_down': fields.boolean('Shut Down'),
'company_id11': fields.many2one('res.company', 'Substation', required=True),
#'place' : fields.many2one('asset.parentlocation','Substation Location',),
'assetmodelid_add' : fields.many2one('agile.portfolio1','Asset Model Code',),
'folio_num1' : fields.many2one('asset.asset','Folio Num',),
'inspection_name' : fields.many2many('asset1.inspection','asset1_inspection_rel','super_id','asset1_inspection_id','Inspection Type'),
'insp_rslt' : fields.one2many('feedback.tree','insp_rsltts','Inspection Result',),
'same_as':fields.boolean(),
}
_defaults = {
'number' : lambda self,cr,uid,context={}:self.pool.get('ir.sequence').get(cr,uid,'calendar.event'),
'sht_down': False,
}
def onchange_same_as(self,cr, uid, ids,same_as_above,inspec_type,context=None):
if same_as:
return {'value':{'shutdown':frequency,}}
return {'value':{}}
class feedback_tree(osv.Model):
_name = 'feedback.tree'
STATUS = [
('done', 'Completed'),
('notdone', 'Not-Completed'),
]
_columns = {
'folionum' : fields.many2one('calendar.event','Task Name',),
#'folio_number' : fields.many2one('asset.asset','Folio number',),
'inspec_type' : fields.char('Inspection Type',),
'insp_msr1' : fields.many2one('results.measure','Attributes',),
'valuess' : fields.char('Values'),
'status' : fields.selection(STATUS,'Status'),
'insp_rsltts': fields.many2one('feedback.form','Result Id'),
}
The follwoing code is .xml file for above inherited class
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Form & Tree View for Visitor Register -->
<record model="ir.ui.view" id="inherit_calaner_event">
<field name="name">calender.event.inherit</field>
<field name="model">calendar.event</field>
<field name="inherit_id" ref="calendar.view_calendar_event_form"/>
<field name="arch" type="xml">
<xpath expr="//form/sheet/div[#class='oe_title']" position="after">
<group>
<!-- <field name="number"/>-->
<field name="company_id11"/>
<!--<field name="asset_catg_id"/>
<field name="area_id" widget="selection"/>-->
<field name="assetmodelid_add" />
<field name="folio_num1" domain="[('assetmodelid_add', '=', assetmodelid_add)]"/>
<field name="inspection_name" domain="[('assetmodelid_add', '=', assetmodelid_add)]"/>
<field name="sht_down"/>
</group>
</xpath>
<xpath expr="//form/sheet/notebook/page/group/group/field[#name='location']" position="after">
<group>
</group>
</xpath>
<xpath expr="//form/sheet/notebook/page/group/group/field[#name='categ_ids']" position="replace">
<field name="tags"/></xpath>
<xpath expr="//form/sheet/div/label[#string='Attendees']" position="replace">
<label string="Attendees" class="oe_edit_only"/></xpath>
<xpath expr="//form/sheet/div/h2" position="replace">
<field name="user_id"/></xpath>
<xpath expr="//form/sheet/notebook/page[#string='Meeting Details']" position="after">
<page string="Feedback">
<field name="insp_rslt" >
<tree string="Feedback" editable="bottom" >
<field name="folionum" />
<field name="insp_msr1"/>
<field name="valuess"/>
<field name="status"/></tree></field>
</page>
</xpath>
</field>
</record>
<record id="calendar_view_calendar_event_search" model="ir.ui.view">
<field name="name">calender.event.view</field>
<field name="model">calendar.event</field>
<field name="inherit_id" ref="calendar.view_calendar_event_search"/>
<field name="arch" type="xml">
<xpath expr="//search/filter[#name='message_unread']" position="after">
<filter string="Shut Down" name="sht_down" domain="[('sht_down','=',True)]"/>
<filter string="No Shut Down" name="sht_down" domain="[('sht_down','=',False)]"/></xpath>
</field>
</record>
</data>
</openerp>
My Requirement is First I will select inspection_name field in calender.evnt class. In feedback.tree I have one column inspc_type this field has to give only same list as many2one of inspection_name which assigned for this instance.
Example: inspction_name has A,B,C
in feedback.tree for inspec_type should get only A,B,C not all inspection_name which stored before this record.
-----------------Edited------------------------------
I have calendar.event.py
'inspection_name' : fields.many2many('asset1.inspection','asset1_inspection_rel','super_id','asset1_inspection_id','Inspection Type'),
.xml file of calendar.event.py
<field name="assetmodelid_add" />
<field name="folio_num1" domain="[('assetmodelid_add', '=', assetmodelid_add)]"/>
<field name="inspection_name" domain="[('assetmodelid_add', '=', assetmodelid_add)]"/>
I want same filter in feedback.tree. When I do domain filter its showing assetmodelid_add doesn't exist. ya because in feedback.tree we don't have this field so how to crack this
'inspec_type': fields.char('Inspection Type',),
field has to be done domain filter for this class for 'inspec_type' field

Why Wizard view cannot find the model?

i make a simple wizard in odoo and i want to show the wizard but, my model can't found of my view . what wrong with my simple code?
This my wizard
class RequestReceiveWizard(models.TransientModel):
_name ='estate.nursery.requestreceivewizard'
# def _default_session(self):
# return self.env['estate.nursery.requestline'].browse(self._context.get('request_id'))
name=fields.Char("Wizard name")
request_id=fields.Many2one('estate.nursery.request')
requestline_ids = fields.Many2many("estate.nursery.requestline")
And this my view :
<!--request Wizard -->
<record model="ir.ui.view" id="request_view_wizard">
<field name="name">Request receive wizard</field>
<field name="model">estate.nursery.requestreceivewizard</field>
<field name="arch" type="xml">
<form string="Wizard For Request Received">
<group collspan="2" col="2">
<field name="name"/>
<field name="requestline_ids"/>
</group>
<footer>
<button name="Apply" type="Action" string="Apply" >Apply</button>
<button special="cancel" string="Cancel">Cancel</button>
</footer>
</form>
</field>
</record>
<act_window id="received_request_wizard" name="Request Wizard"
src_model="estate.nursery.request"
res_model="estate.nursery.requestreceivewizard"
view_mode="form"
target="new"
multi="True" key2="client_action_multi"/>
What wrong with my simple code? And this my error
Model not found: estate.nursery.requestreceivewizard
act_window replace to this record and try this code:
<record id="received_request_wizard" model="ir.actions.act_window">
<field name="name">estate.nursery.request</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">estate.nursery.request</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="request_view_wizard"/>
<field name="target">new</field>
</record>
May be It's work for you...

except_orm: ('ValidateError', u'Error occurred while validating the field(s) arch: Invalid XML for View Architecture!')

i created a wizard and now its giving me the above error , it took me ages but couldn't sorted it , between am doing wizard for the first time in Openerp . please help thanks in advance . here is my code
class test_wizard(osv.osv_memory):
_name="test.wizard"
_description="will generate dynamic views, that's all"
_columns={
'age_text': fields.text('text age'),
}
def next(self,cr,uid,ids,contet=None):
test_obj=self.pool.get('test.chbc')
for wiz in self.browse(cr,uid,ids):
if wiz.age_text <=5:
raise osv.except_osv('user error, please slect something , know what')
limit= datetime.date.today()-datetime.timedelta(days=wiz.age_text)
ids_to_del=test_obj.search(cr,uid,[('create_date','<',
limit.striftime('%y-%m-%d 00:00:00'))],context=context)
test_obj.unlink(cr,uid,ids_to_del)
return{}
test_wizard()
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="test_wizard" model="ir.ui.view">
<field name="name">test.wizard.form</field>
<field name="model">test.wizard</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Test wizard ">
<label colspan="4" string="select age please , am in a hurry "/>
<field name="age_text" string="Age(days)"/>
<group col="4" colspan="4">
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-ok" name="next" string="Next" type="object"/>
</group>
</form>
</field>
</record>
<record id="action_test_wizard" model="ir.actions.act_window">
<field name="name">Next</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">test.wizard</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</openerp>
Change you model name mean give the name of the class where you want to put it
<field name="model">test.wizard</field>