How to get parent id - odoo

I want to recuper contrat_parent_id in model 'facturation.lot' for filtring.
Models :
class Contrat(models.Model):
_name = 'facturation.contrat'
code = fields.Char(string='Numéro Contrat', required=True)
contrat_parent_id = fields.Many2one('facturation.contrat', string='Numéro Contrat Client')
class Lot(models.Model):
_name = 'facturation.lot'
_description = 'Lots'
contrat_id = fields.Many2one('facturation.contrat', ondelete='cascade')
articlecontrat_ids = fields.Many2many('facturation.articleouvrage',string='Article Lot')
class ArticleOuvrage(models.Model):
_name = 'facturation.articleouvrage'
_description = 'Ligne Articles'
article_id = fields.Many2one('facturation.article',string='Article')
ouvrage_id = fields.Many2one('facturation.ouvrage',string="Ouvrage")
View :
<record id="lot_view_form" model="ir.ui.view">
<field name="name">facturation.lot.form</field>
<field name="model">facturation.lot</field>
<field name="arch" type="xml">
<form>
<sheet>
<field name="lib_lot" string="Lot"/>
<field name="contrat_id" options="{'no_create':True}"></field>
<notebook>
<page string="Composants" >
<field name="articlecontrat_ids" domain="[('ouvrage_id.contrat_id.id','=',1)]" options="{'no_create':True}" >
<tree>
I want to recuper contrat_parent_id in model 'facturation.lot' for filtring.
thanks

Related

Odoo 13 - multiple model and menus in same module

I'm new to odoo and learning developing custom module. Trying to develop an contact management app for company and person. Following are the files and code structure:
#models.py
from odoo import models, fields, api
class company(models.Model):
_name = 'cs_contact.company'
_description = 'Model for create company profile.'
name = fields.Char('Company name', required=True)
country_id = fields.Many2one('res.country', string='Country', help='Select Country', ondelete='restrict', required=True)
ho_address = fields.Text('HO address')
website = fields.Char('Website')
courier_account = fields.Char('Courier Account')
email = fields.Char('Email')
class person(models.Model):
_name = 'cs_contact.person'
_description = 'Model for create person contact.'
name = fields.Char('Full Name', required=True)
country_id = fields.Many2one('res.country', string='Country', help='Select Country', ondelete='restrict', required=True)
email = fields.Char('Email')
im_id = name = fields.Char('Instant messaging ID (Skype/line)')
worked_before = fields.Selection([
('Yes', 'Yes'),
('No', 'No'),
], string="Worked Before?")
how_we_meet = fields.Selection([
('Fair', 'Fair'),
('Email', 'Email'),
('Agent', 'Agent'),
], string="How we meet?")
quantity = fields.Integer(string='Quantity')
note = fields.Text('Note')
Views looke like:
#views.xml
<odoo>
<data>
<!-- explicit list view definition -->
<record model="ir.ui.view" id="cs_contact.list">
<field name="name">cs_contact list</field>
<field name="model">cs_contact.person</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="country_id" />
<field name="email"/>
</tree>
</field>
</record>
<record id="view_cs_contactsearch" model="ir.ui.view">
<field name="name">cs_contact list</field>
<field name="model">cs_contact.person</field>
<field name="arch" type="xml">
<search string="Search contacts">
<field name="name"></field>
<field name="country_id"></field>
<field name="email"></field>
</search>
</field>
</record>
</data>
</odoo>
Menu looks like:
#menu.xml
<odoo>
<act_window id="action_company" name=" Company Contacts" res_model="cs_contact.company" view_mode="tree,form" />
<menuitem id="contact_root" name="Contacts" sequence='-1' />
<menuitem id="contact_company" name="Company" parent="contact_root" action="action_company" sequence="-1" />
</odoo>
It's working fine for company contact. Now I'm not getting how to create top menu for person and define view. This is the the design I want. I tried various method from blogs but didn't work. Please help me out.
You have added country_id in cs_contact.person view definition but the field does not exist. Remove it from the view definition or declare it in the corresponding model.
To add the Person menu item next to the Company menu item, you just need to use the same parent and higher sequence number then connect it to the corresponding action.
Example:
<act_window id="action_person" name="Persons" res_model="cs_contact.person" view_mode="tree,form"/>
<menuitem id="contact_person" name="Person" parent="contact_root" action="action_person" sequence="2"/>

how to use virtual records of one2many field in a many2one field of the same model

In the Sale Order form I added a one2many field named booking_ids which appears within an editable tree view
Within the tree of the order lines, I added a many2one column named booking_id so each orderline have a booking_id.
While in edit mode or create mode of the sale.order how is it possible to show the virtual records added in the booking_ids field within the many2one field which is only showing the records within the database without the virtual ones that are being created.
class SaleOrder(models.Model):
_inherit = 'sale.order'
booking_ids = fields.One2many('sale.booking', 'order_id', string='Bookings')
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
booking_id = fields.Many2one('sale.booking', string="Booking")
class Booking(models.Model):
_name = 'sale.booking'
event_id = fields.Many2one('sale.event', String='Event', required=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)]})
name = fields.Char(compute='_get_name')
event_date = fields.Date(string='Event Date')
order_id = fields.Many2one('sale.order', string='Order Reference', ondelete='cascade', index=True, copy=False, readonly=True)
order_line_ids = fields.One2many('sale.order.line', 'booking_id', string='Order Lines')
XML
<xpath expr="//field[#name='payment_term_id']/.." position="after">
<group>
<field name="booking_ids">
<tree editable="bottom">
<field name="event_id"/>
<field name="event_date" string="Date"/>
<field name="order_line_ids" widget="many2many_tags" readonly="1"/>
</tree>
</field>
</group>
</xpath>
<xpath expr="//tree//field[#name='product_id']" position="after">
<field name="order_id" invisible="1"/>
<field name="booking_id" domain="['|', ('order_id', '=', None), ('order_id', '=', order_id)]"/>
</xpath>
<xpath expr="//field[#name='order_line']//field[#name='product_id']/.." position="after">
<field name="order_id" invisible="1"/>
<field name="booking_id" domain="['|', ('order_id', '=', None), ('order_id', '=', order_id)]"/>
</xpath>
thank you
Use a preload in Python code. This will solve your problem.

Odoo. Tree/form display field data

I have some problem with tree/form view in Odoo.
My model have such classes: https://yadi.sk/d/sCLVo3gHtbVEu
class URLList(models.Model):
_name = 'webvisitorcalc.url_list'
url = fields.Char(string="URL", required=True)
url_parametes = fields.Char(string="URL parameters") #пераметры URLб всё что идёт после ?
target_session_id = fields.One2many('webvisitorcalc.session_visitor', 'target_url_ids', string='Target URL')
site_trip_prevouse_id = fields.One2many('webvisitorcalc.site_trip', 'url_prevouse_ids', string='Prevouse URL')
site_trip_current_id = fields.One2many('webvisitorcalc.site_trip', 'url_current_ids', string='Current URL')
remote_sites_id = fields.One2many('webvisitorcalc.remote_sites', 'site_url_ids', string='Remote site page with URL')
remote_sites_target_url_id = fields.One2many('webvisitorcalc.remote_sites', 'target_url_ids', string='URL on remote site page')
#api.multi
def url_exist(self, cr, SUPERUSER_ID, urlForCheck):
_logger.error("Check URL exist in DB ")
result = False
if (self.search_count(cr, SUPERUSER_ID, [('url', '=', urlForCheck)])>0):
result = True
return result
class SiteTrip(models.Model):
_name = 'webvisitorcalc.site_trip'
session_ids = fields.Many2one('webvisitorcalc.session_visitor', string='Session ID', index=True)
url_prevouse_ids = fields.Many2one('webvisitorcalc.url_list', string='Prevouse URL', index=True)
url_current_ids = fields.Many2one('webvisitorcalc.url_list', string='Current URL', index=True)
Template for this model: https://yadi.sk/d/Ob0o65PutbVFA
<record model="ir.actions.act_window" id="site_trip_list_action">
<field name="name">Site trip</field>
<field name="res_model">webvisitorcalc.site_trip</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create the first record for site trip
</p>
</field>
</record>
<record model="ir.actions.act_window" id="url_list_list_action">
<field name="name">URL list</field>
<field name="res_model">webvisitorcalc.url_list</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create the first url
</p>
</field>
</record>
<record model="ir.ui.view" id="site_trip_tree_view">
<field name="name">site_trip.tree</field>
<field name="model">webvisitorcalc.site_trip</field>
<field name="arch" type="xml">
<tree string="URL list tree">
<field name="session_ids"/>
<field name="url_prevouse_ids" string="PrevURL">
</field>
<!--<field name="url_prevouse_ids"/>-->
<field name="url_current_ids"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="url_list_tree_view">
<field name="name">url_list.tree</field>
<field name="model">webvisitorcalc.url_list</field>
<field name="arch" type="xml">
<tree string="URL list tree">
<field name="url"/>
<field name="url_parametes"/>
</tree>
</field>
</record>
<menuitem id="site_trip_menu" name="Site trip" parent="webvisitorcalc_menu"
action="site_trip_list_action"/>
<menuitem id="url_list_menu" name="URL list" parent="webvisitorcalc_menu"
action="url_list_list_action"/>
Screenshots are here:
Tree view for class SiteTrip
http://i.stack.imgur.com/FjRDK.png
Form view for class SiteTrip
http://i.stack.imgur.com/uDbOp.png
Tree view for class URLList
http://i.stack.imgur.com/tXzqL.png
Form view for class URLList
http://i.stack.imgur.com/oVnqg.png
As you see URLList displayed fine. For class SiteTrip present problem. Field is displaying not data from URLList. This is field stored element such webvisitorcalc.url_list.ID (array?). How I can display real data in this field (for example URL: http://some-site.com/page.html)?
URL in URLList must be uniq. SiteTrip must have stored only ID of URLList record.
UPD:
class RemoteSites(models.Model):
_name = 'webvisitorcalc.remote_sites'
site_id = advert_company_id = fields.One2many('webvisitorcalc.site_list', 'remote_sites_ids', string='Site')
site_url_ids = fields.Many2one('webvisitorcalc.url_list', string='URL page ')
target_url_ids = fields.Many2one('webvisitorcalc.url_list', string='URL target page')
You obviously have no name field on your webvisitorcalc.url_list model. Odoo needs this to use it as name in webclient wherever you use this model as e.g. many2one field or in the breadcrumb navigation.
So either you define a name field or you set _name on your class with another field identifier.
You can also (re-)define the method display_name on your model (enough examples in Odoo code) where you can do more cool stuff with the record display name :-)

Access inherited field of a model from a view

I'm working on OpenERP7, trying to access a related field located in a parent model of said related model. If someone at this point understand something, you're way smarter than I am, so i will just put the example of what i'm trying to achieve :
My model :
class trench(osv.osv):
_name = 'trench'
_inherit = 'common'
_columns = {
'trench_lines': fields.one2many('trench.line', 'trench_id', 'Trench Lines'),
'trench_depth': fields.one2many('trench.depth', 'trench_id', 'Trench Depth'),
}
trench()
class trench_common(osv.osv):
_name = 'trench.common'
def compute_vals(self, cr, uid, ids, field_name, arg, context):
...
def on_change_values(self, cr, uid, ids, context=None):
...
_columns = {
'trench_id': fields.many2one('trench', 'Trench', ondelete='cascade', required=True),
'length' : fields.function(compute_vals, type='float', string="Length", method=True, store=True),
}
trench_common()
class trench_line(trench_common):
_name = 'trench.line'
_inherit = 'trench.common'
trench_line()
class trench_dig_common(trench_common):
_name = 'trench.dig.common'
_inherit = 'trench.common'
_columns = {
'length' : fields.float('Length', digits=(6,3)),
'height' : fields.float('Height', digits=(6,3)),
'total_m3' : fields.float('Total m3', digits=(6,3)),
'observation' : fields.text('Observation'),
}
trench_dig_common()
class trench_depth(trench_dig_common):
_name = 'trench.depth'
_inherit = 'trench.common'
trench_depth()
My view :
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="trench_form" model="ir.ui.view">
<field name="name">trench.form</field>
<field name="model">trench</field>
<field name="inherit_id" ref="common_form" />
<field name="arch" type="xml">
<group string="Worksite" position="after">
<separator string="Progress" />
<field name="trench_lines">
<tree editable="bottom">
<field name="length"/>
</tree>
</field>
<separator string="Cumulate Length" />
<field name="total_length"/>
<separator string="Trench Particularity" />
<notebook>
<page string="Surdepth">
<field name="trench_depth">
<tree editable="bottom">
<field name="height"/>
</tree>
</field>
</page>
</notebook>
</group>
</field>
</record>
</data>
</openerp>
My error :
except_orm: ('View error', u"Can't find field 'height' in the following view parts composing the view of object model 'qhse.trench':\n * trench.form\n\nEither you wrongly customized this view, or some modules bringing those views are not compatible with your current data model")
2015-05-21 07:56:28,631 13918 ERROR ahak_production openerp.tools.convert: Parse error in trench_view.xml:4:
So, i'm figuring i can't access a field of a model with so many layers, but is there a way to achieve this, and if so, how? I'm trying to make something DRY, but always end up duplicating code with OpenERP.
Thanks for reading.
You need to define your last class as below.
class trench_depth(trench_dig_common):
_name = 'trench.depth'
_inherit = 'trench.dig.common'
trench_depth()
Then after you can access all fields which are available inside "trehch.dig.common" model.

openerp to download attendance machine

Please me to solve this problems. I'm using openerp to download attendance machine and insert that data into my table but i have to view the data from the machine into grid using using one2many field how could I passing the data after I downloaded this my script. I'm using zkclock module to get the data.
hr_machinezem560.py
....
import zkclock
import from osv import osv, fields
....
class hr_machinezem560(osv.osv):
_name = "hr_machinezem560"
_columns = {
'date_from':fields.date("Date From"),
'date_to':fields.date("Date To"),
'proses_id':fields.one2many('hr.machinezem560_details', 'proses_id', 'Prosess Download')
}
def action_download(self, cr, uid, ids, context=None):
domain = {}
params = self.browse(cr, uid, ids, context=context)[0]
start_date = date_object = datetime.datetime.strptime(params.date_from, '%Y-%m-%d')
end_date = date_object = datetime.datetime.strptime(params.date_to, '%Y-%m-%d')
date_list = [dt.strftime("%Y-%m-%d") for dt in rrule(DAILY, dtstart=start_date, until=end_date)]
host = '192.168.1.201'
port = 4370
password = ''
s, r = zkclock.connect(host, port, password)
r = zkclock.disable(s, r)
r, userdata = zkclock.get_user_data(s, r)
r, logdata = zkclock.get_log_data(s, r)
r = zkclock.enable(s, r)
r = zkclock.disconnect(s, r)
if userdata and logdata:
logdata = zkclock.assemble_log_data_from_packets(logdata)
users = zkclock.unpack_users_to_dict(userdata)
logging.debug(users)
loglist = []
while((len(logdata)/LOG_DATA_SIZE) >=1):
loglist.append(logdata[:LOG_DATA_SIZE])
logdata = logdata[LOG_DATA_SIZE:]
logs = []
for i in loglist:
log_entry = zkclock.unpack_log(i)
timestamp = zkclock.decode_time(log_entry.time)
verification_method = log_entry.decode_verified()
try:
user_name = users[log_entry.uid].name
except KeyError:
user_name = "Unknown user: %s" % log_entry.uid
logs.append([timestamp,str(log_entry.uid), verification_method])
attance_det = self.pool.get("hr.machinezem560_details")
for log in logs:
if log[0].strftime('%Y-%m-%d') in date_list:
values = {
'employee': log[1],
'timesheet': log[0],
'status':log[2]
}
attance_det.create(cr, uid, values, context=context)
hr_machinezem560()
class hr_machinezem560_details(osv.osv):
_name = "hr.machinezem560_details"
_columns = {
'employee': fields.char("Employee Name", size=80),
'timesheet':fields.datetime("Time Sheet"),
'status': fields.char("Status", size=10),
'proses_id':fields.many2one('hr_machinezem560','Prosess Download')
}
hr_machinezem560_details()
view:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="hr_batch_machine_view_form" model="ir.ui.view">
<field name="name">Attendance Machine Batch Process</field>
<field name="model">hr_machinezem560</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Download ZEM560 Data" version="7.0">
<group name="up">
<label for="date_from" string="Timesheet Period"/>
<div>
<field name="date_from" class="oe_inline"/> to <field name="date_to" class="oe_inline"/>
<div class="oe_right oe_button_box" name="right_top_button">
<button name="action_download" string="Download" type="object"/>
</div>
</div>
</group>
<group name="down">
<field name="proses_id" colspan="3" nolabel="1" readonly="1">
<tree string="Attendance Data">
<field name="employee"/>
<field name="timesheet"/>
<field name="status"/>
</tree>
</field>
</group>
</form>
</field>
</record>
<record id="action_batch_hr_machine_view" model="ir.actions.act_window">
<field name="name">Attendance Machine Batch Proses</field>
<field name="res_model">hr_machinezem560</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem
id="menu_hr_absensiRoot"
name="Absensi"
sequence="3"
parent="hr.menu_hr_root"
/>
<menuitem
id="menu_hr_mesin_Absensi"
name="Mesin Absensi ZEM560"
sequence="1"
parent="menu_hr_absensiRoot"
action="action_batch_hr_machine_view"
/>
</data>
</openerp>