I'm looking for a way to filter subsequent selections depending on which ones of them are filled.
I want to have a tree-like structure for setting locations as such:
=>Country
==>State
===> District
So say you enter the district first, you'd only have one choice in the other two.
Say you enter the country first, you'd have a more limited selection of the latter two.
I know i should be going with many2one fields but all i can get working at this point is multiple selections that turn visible/invisible which is highly inefficient.
I'm fairly new to Odoo and am finding the docs somewhat lacking. Any help would be appreciated!
I would play around with many2one fields and domains using the operator =?
For example:
class MyModel(models.Model):
_name = "my.model"
country_id = fields.Many2one(comodel_name="res.country")
state_id = fields.Many2one(comodel_name="res.country.state")
disctrict_id = fields.Many2one(comodel_name="res.country.state.district")
I don't know if the model names are correct, but you should already know them.
And in the view use the domain with the mentioned operator:
<field name="country_id"
domain="[('state_ids', '=?', state_id),('state_ids.district_ids', '=?', district_id)]" />
<field name="state_id"
domain="[('country_id', '=?', country_id),('district_ids', '=?', district_id)]" />
<field name="disctrict_id"
domain="[('state_id', '=?', state_id),('state_ids.country_id', '=?', country_id)]" />
I didn't make any tests on those AND conditions/domains. And i don't know the structure of those 3 models. My assumption is, that there is a one2many or many2many relation on res.country -> res.country.state and res.country.state -> res.country.state.district.
Related
Say, in odoo I have a Model A with a many2one field to model B.
Model B has a field "city".
Now I want to create a form for model A in which I want the "city" field of model B.
I can do this by adding the city field of Model B to model A as well and giving it the related flag.
b_city = fields.Char(related='b_id.city', depends=['b_id'])
But I dont like this because than I have to add this field to my model. I would prefer if can do this without creating this field. Is this possible?
-----------Edit---------------
Something that I'm looking for is like this:
<page string="Offers">
<field name="offer_ids">
<tree>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="accept" type="object" icon="fa-check"/>
<button name="reject" string="X" type="object" icon="fa-xmark"/>
<field name="status"/>
</tree>
</field>
</page>
This a page in a form where the corresponding model has a one2many field which is being displayed in a tree view. Now, I want to the other way around. I want to display in a form some fields of another model to which the there is a many2one field.
Is it possible to do this in the the xml view?
There is no way to do that in Odoo unless using related field.
Check Odoo ORM documentation
USING PYTHON CODE :
Example 1:
invoice_partner_icon = fields.Char(compute='_compute_invoice_partner_display_info', store=False, compute_sudo=True)
#api.depends('partner_id', 'invoice_source_email')
def _compute_invoice_partner_display_info(self):
for move in self:
vendor_display_name = move.partner_id.display_name
if not vendor_display_name:
if move.invoice_source_email:
vendor_display_name = _('From: ') + move.invoice_source_email
move.invoice_partner_icon = '#'
else:
vendor_display_name = _('Created by: %s') % (move.sudo().create_uid.name or self.env.user.name)
move.invoice_partner_icon = '#'
else:
move.invoice_partner_icon = False
move.invoice_partner_display_name = vendor_display_name
OR USING UI :
As admin, You can create a new field in Model B which is related to model A without storing it in the database : in App Settings, switch to debug mode by adding "?debug=1" in your url, reload the page, then go to the new Technical Menu-tab > Fields.
Then, create your new related field using the panel "Advanced Properties" Related Field: ...
and then uncheck [ ] Stored
I defined a Many2many field for ir.attachemnt. The problem is i can' see the Add an item link to add the records.
*.py
attachment_ids = fields.Many2many('ir.attachment', string='Attachments')
*.xml
<notebook>
<page string='Attachments'>
<field name="attachment_ids"/>
</page>
</notebook>
I also tried this:
<field name="attachment_ids" widget="many2many" />
Any idea?
There is only two thing that make Odoo behave like this.
Your view is in edit mode but I think I'm seeing a place holder comments this means it's not the case
Your user is not allowed to create an ir.attachment which is more likely not the case
Your field is readonly.
If not one of this cases these is wired but if you didn't understand what happen you can force that link to appear by using embedded tree with create attribute set to true
<field..... >
<tree create='1'>
....
It'a a bug. If you have more many2many fields in the some class and for one of this you haven't right access rules, all the many2many fields are showed in readonly mode.
When I edit a record from this field (code below), it doesn't save for some reason. It's a computed field, linking to res.partner records. If I edit it and click save, it doesn't save at all (no changes in the database and/or if I hard refresh the page). Does someone see something here that I'm missing? If I can't edit it via what I'm expecting, is there another way to do this? The reason I do a computed field and not a domain on child_ids is because child_ids field with a domain doesn't seem to work properly with this domain.
Model
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", readonly=False)
#api.multi
#api.depends('child_ids')
def _get_contact_ids(self):
for company in self:
if company.child_ids:
company.contact_ids = company.child_ids.search([('is_location', '=', False), ('parent_id', '=', company.id), ('type', '=', 'contact')])
View
<field name="contact_ids" string="Contacts">
<tree create="true" delete="false" edit="true" default_order="create_date">
<field name="name"/>
<field name="phone"/>
<field name="email"/>
</tree>
</field>
Update
Added this per ideas, but it didn't work. Keep in mind, this is on a model that inherits res.partner.
activity_contact_id = fields.Many2one('res.partner', string="Contact")
contact_ids = fields.One2many(
comodel_name='res.partner',
inverse_name='activity_contact_id',
compute="_get_contact_ids",
readonly=False,
stored=True
)
Computed fields in Odoo are not stored by default, you need to set store=True in order to save the fields to database.
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", stored=True, readonly=False)
To store a one2many value in database you need the inverse_name on the other model.
I mean that you need to create a many2one field to save the id of the current record
in the co_model. (o2m needs m2o you cannot store the values without m2o !! remember this role)
don't use one2many field use many2many field it is better.
contact_ids = fields.Many2many(comodel_name='res.partner',
relation="your_model_res_partner_rel", # always mention the name of the relation good practice
column1 = "you_mode_id",
column2 = "partner_id",
compute="_get_contact_ids",
store=True) # make your field stored no need for readonly it's by default
#api.depends('child_ids')
def _get_contact_ids(self):
""" always explain what the method do here good practice for team work"""
for company in self:
if company.child_ids:
# break you line when it's to long to be readable
ids = company.child_ids.search([('is_location', '=', False),
('parent_id', '=', company.id),
('type', '=', 'contact')]).ids
company.contact_ids = [(6, False, ids)] # replace all records by the new ids
Suppose there is a field 'A' and has an fields declaration say
A:fields.many2one('new.new')
and then based on any condition, the same field should get a different relation like
if condition:
A:fields.many2one(old.old)
Is it possible?
AFAIK the answer is No. You cannot change at runtime the comodel of a m2o field. It would require to change the Foreign Key and other IR relations which are created when the module is installed/updated.
Maybe you can achieve something similar by creating both
A: fields.many2one('old.old')
B: fields.many2one('new.new')
C: fields.Boolean()#condition, eventually computed
and then using A or B depending on your condition.
for the view part:
<field name="C" invisible="1"/>
<field name="A" attrs="{'invisible': [('C','=',False)]}"/>
<field name="B" attrs="{'invisible': [('C','=',True)]}"/>
C is always invisible, A and B visibility depends on C
Im having problems with creating a filter on stock.picking object. Just recently i build a simple "privilege relay" - in each stock location you can define "Assigned User Group", thanks to that users that are in particular group can or cannot confirm moves for or out of the location.
Stock.picking:location_id -> assigned_user_group -> users
Now I would like to create a filter (later to be set default) on stock picking tree view that will show only the moves which locations (source location and destination location; i use them in stock.picking object) can be managed by a viewing user.
By far I wrote a filter that looks like that:
<record id="view_picking_internal_search_pl" model="ir.ui.view">
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_internal_search"/>
<field name="arch" type="xml">
<filter icon="terp-dialog-close" name="done" string="Done" domain="[('state','=','done')]" help="Pickings already processed" position="after">
<filter icon="terp-check" name="locgroup" string="Location Group" domain="[('user_id','in',[user.id for user in location_id.user_group.users])]" context="{'group_by':'date'}"/>
</filter>
</field>
</record>
I also added field location_id to the tree view.
But Im still getting an error (after choosing the filter) that even google doesnt know anything about:
TypeError: results.group_by is undefined
My questions are:
By looking on domain in filter field - what am i doing wrong?
Is something like that even possible?
I will gladly welcome any help.
Firstly, i think your domain is not correct, it could have been :
[('user_group.users.id', '=', uid)]
(because the first element of the tuple is a field on the model; and uid is a special value supplied in search views)
Next, This error :
TypeError: results.group_by is undefined
Seems to be a Javascript Error (coming from openerp-web interface), it often throws error like that when it receives unexpected values (when we make a mistake defining a view for example).
can you tell us if using the domain above solved your problem ?
NB: does your field user_group is a required field ? If not, i think the domain above won't display picking where user_group is not set, if you want to display picking where user_group is not set too, you can set a domain like that:
['|',('user_group.users.id', '=', uid), ('user_group','=',False)]
Regards