How to filtred when I have different models - odoo

This is the .py:
class Travsup(models.Model):
_name = 'facturation.travsup'
attachment_id = fields.Many2one('facturation.attachement')
ouvrage_id = fields.Many2one('facturation.ouvrage',domain="?????")
class Attachement(models.Model):
_name = 'facturation.attachement'
travSup_ids = fields.One2many('facturation.travsup','attachment_id')
contrat_id = fields.Many2one('facturation.contrat',string='Contrat',required=True)
class Contrat(models.Model):
_name = 'facturation.contrat'
ouvrage_ids = fields.One2many('facturation.ouvrage', 'contrat_id', string='Ouvrages')
class Ouvrage(models.Model):
_name = 'facturation.ouvrage'
contrat_id = fields.Many2one('facturation.contrat', string='Contrat')
How to filtred ouvrage, when i change contrat_id of attachement model i get all
ouvrage of model ouvrage of the same contrat_id of ouvrage model.
if Attachement.contrat_id == Ouvrage.contrat_id then travSup_ids.ouvrage_id = Ouvrage.id

You can specify the domain like this in your form view:
<!-- this field must appear in the form so we can use it in domain -->
<field name="contrat_id"/>
<field name="travSup_ids">
<tree....>
<field name="ouvrage_id" domain="[('contrat_id', '=', parent.contrat_id)]/>
</tree>
<form>
....
....
<field name="ouvrage_id" domain="[('contrat_id', '=', parent.contrat_id)]/>
</form>
</field>
In embaded form or tree you can access the field of the current form by
using parent.field_name

Related

I am trying to inherit and add one2many fields in res.partner model . It shows errror : Invalid field 'same_vat_partner_id' on model 'vehicle.brand'

inherited " res.partner " and added a page (editable tree) in notebook section, but when clicking on "Add a line" it is showing below error:
Invalid field 'same_vat_partner_id' on model 'vehicle.brand'
My code to inherit res.partner and add One2many fields in it :
from odoo import api, fields, models
class CustomContacts(models.Model):
_inherit = "res.partner"
x_brand_ids = fields.Many2many('res.partner.line', 'x_brand_id', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('vehicle.model', string="Model Name")
My code of vehicle.brand model :
from odoo import api, fields, models
class BrandCreate(models.Model):
_name = "vehicle.brand"
_description = "Customer"
name = fields.Char(string='Brand Name', required=True)
My code of vehicle.model model :
from odoo import api, fields, models
class ModelName(models.Model):
_name = "vehicle.model"
_description = "Models"
name = fields.Char(string='Model Name', required=True)
My code of view :
<?xml version="1.0" encoding="utf-8"?>
<record id="view_partner_form_inherited" model="ir.ui.view">
<field name="name">res.partner.inherited</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#name='internal_notes']" position="after">
<page string="Vehicle Details">
<field name="x_brand_ids"></field>
<field name="x_model_ids"></field>
</page>
</xpath>
</field>
</record>
Getting view inside view as mentioned in below images :
view 1
view 2
You needs to understand the Many2many and One2many fields.
For many2many you check the required argument is the model name only. and other are optional. (Filling Many2many field (odoo 8))
x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")
For adding One2many we need many2one with that same model.
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
For this you need x_model_id Many2one of res.partner field under res.partner.line
You need to like below:
class CustomContacts(models.Model):
_inherit = "res.partner"
x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('res.partner', string="Model Name")
When using a One2many field the inverse_name (x_model_id in your example) is equal to the current record and Odoo tries to recompute the partner vat using vehicle.model record in place of a res.partner record and this is why you are getting that error.
To fix it just define the inverse name in vehicle.brand model and use it in x_model_ids field instead of x_model_id.
Example:
class CustomContacts(models.Model):
_inherit = "res.partner"
x_model_ids = fields.One2many('res.partner.line', 'model_id', string="Model Name")
class CustomContactsPage(models.Model):
_name = "res.partner.line"
model_id = fields.Many2one('res.partner')
Edit:
If you want to edit the One2many field in place, define it inside the form view like following:
<field name="x_model_ids">
<tree editable="bottom">
</tree>
</field>
For the Many2many fields, Odoo will open a dialog to select or create vehicle brands.

Why isn't this domain giving me results?

So I'm making this localization kind of module for Lebanon and am currently doing the location bit.
The intended functionality is to have the choices of governate, district and region restrict the choices the user can make by filtering out the irrelevant options.
Here are the models:
# -*- coding: utf-8 -*-
from odoo import models, fields, api
# Base model
class LebaneseLocation(models.Model):
_inherit = "res.partner"
governate_id = fields.Many2one("lebanon.governate", "Governate")
district_id = fields.Many2one("lebanon.district", "District")
region_id = fields.Many2one("lebanon.region", "Region")
# Child models with the necessary relations
class Governate(models.Model):
_name = "lebanon.governate"
name = fields.Char()
child_districts = fields.One2many("lebanon.district", "parent_governate",
"Child Districts")
class District(models.Model):
_name = "lebanon.district"
name = fields.Char()
parent_governate = fields.Many2one("lebanon.governate", "Parent Governate")
child_regions = fields.One2many("lebanon.region", "parent_district",
"Child Regions")
class Region(models.Model):
_name = "lebanon.region"
name = fields.Char()
parent_district = fields.Many2one("lebanon.district", "Parent District")
and the view file:
<?xml version="1.0"?>
<odoo>
<record id="view_res_partner_extended_location" model="ir.ui.view">
<field name="name">Contacts Custom</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="category_id" position="after">
<field name="governate_id" domain="[('child_districts','=?','district_id'),('child_districts.child_regions','=?','region_id')]"/>
<field name="district_id"/>
<field name="region_id"/>
</field>
</field>
</record>
</odoo>
And all the data was inputed via two data files, one makes the records without relations and the other updates the same records with relations.
I was testing things out with the Governate domain and it just kept returning nothing.
Is there some way to check what exactly these parameters i'm evaluating are returning? That would help a lot with debugging.
Edit: Here's a screenshot from Studio's menu for the domain, in case that's at all relevant for debugging (I avoid Studio like a plague usually)
Domain Image
I solved my issue by forgoing the XML approach and setting the domains using the onchange api call:
#api.onchange('governate_id', 'district_id', 'region_id')
def update_domains(self):
governate = self.governate_id.name
district = self.district_id.name
region = self.region_id.name
result = {}
result['domain']={
'governate_id':[('child_districts.name','=?', governate),
('child_districts.child_regions.name', '=?', region)],
'district_id':[('parent_governate.name', '=?', governate),
('child_regions.name', '=?', region)],
'region_id':[('parent_district.name', '=?', district),
('parent_district.parent_governate.name', '=?', governate)]
}
return result

How to get parent id

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

Odoo- How to add multiple views using one class

I am using Odoo 10-e. I created a custom class for order
class Order(models.Model):
_name = 'amgl.order'
_description = 'Use this class to maintain all transaction in system.'
name = fields.Char(string='Name',readonly=True)
order_line = fields.One2many('amgl.order_line', 'order_id', string='Order Lines')
total_qty = fields.Float(string='Total Expected')
total_received_qty = fields.Float(string='Total Received Quantity')
customer_id = fields.Many2one('amgl.customer', string='Customers', required=True)
is_pending = fields.Boolean()
date_opened = fields.Datetime('Date Opened', required=True)
date_received = fields.Datetime('Date Received')
I also created a view for this class which show all records in tree view . Now i want to create another view named 'Pending Orders' in which i want to show all order where is_pending is true. I am new maybe that's why i am unable to find any example in Odoo Code base.
For this you don't need to create a new view just create a new menu and action and filter the records using domain.
<record id="action2_...." model="ir.actions.act_window" >
<field name="name"> Action Title </field>
....same as the first action...
<field name="res_model">your.model</fiel>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain">[('is_pending', '=', True)] </field>
</record>
<menuitem ..... action="action2_.." />
NB: action can have properties like domain ,context, view_id, search_view_id, view_ids ... etc best way to learn is read about them and see the code in odoo.

Adding a field in parent view

i am having some problems when i try to add a field in the parent view.
The class is:
class VademecumFraccionamiento(models.Model):
_name = 'farmacia.vademecum_fraccionamiento'
_inherits={
'farmacia.vademecum': 'vademecum_id'
}
hijo = fields.Many2one('farmacia.vademecum_fraccionamiento', string="Artículo hijo", index=True)
vademecum_id = fields.Many2one('farmacia.vademecum', string='Artículo Padre', required=True, ondelete='cascade', index=True)
The xml is:
<record model="ir.ui.view" id="farmacia_vademecum_fraccionamiento_form_view">
<field name="name">farmacia_vademecum_fraccionamiento.form</field>
<field name="model">farmacia.vademecum</field>
<field name="inherit_id" ref="farmacia_vademecum.farmacia_vademecum_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='lalala']" position="after">
<page string="Fracc">
</page>
</xpath>
<xpath expr="//page[#string='Fracc']" position="inside">
<group>
<field name="vademecum_id">
</field>
</group>
</xpath>
</field>
</record>
The error is:
Error details:
The field vademecum_id not exists
I don't know how to solve that.
Thanks in advance
You should refer addons/product/product_view.xml for more help, in which you will get all the answers of your questions related to inheritance.
I would change the code to:
_columns = {
'hijo' : fields.Many2one('farmacia.vademecum_fraccionamiento', string="Artículo hijo", index=True),
'vademecum_id' : fields.Many2one('farmacia.vademecum', string='Artículo Padre', required=True, ondelete='cascade', index=True),
}
This will add the fields to your model
There are two concepts in odoo for field inheritance.
_inherit : can be used in case you want to extend existing model.
Example: adding birth date field in res.partner model
class res_partner(models.Model):
_inherit = 'res.partner'
birth_date = fields.Date('Birthdate')
_inherits : can be used in case you want to adept module field in current model.
Example: Using customer fields in student model,
class Student(models.Model):
_name = 'stundent.student'
_inherits = {'res.partner': partner_id}
partner_id = fields.Many2one('res.partner', 'Partner')
after adding a partner_id field in your model you can use all the fields of partner in xml view of student form & tree.
Hope this helps.