Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to use domains for required fields of many2one when having multiple many2ones?

By
Yenthe
on 4/2/15, 10:24 AM 710 views

Hi guys,

I have two different many2one relations. This are the fields that I define in my model:

     _columns = {
        'product_id': fields.many2one('product.product','Product'),
        'car_id': fields.many2one('fleet.vehicle', 'Car'),
        'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
        'analytic_car_account_id': fields.many2one('account.analytic.account', 'Analytic Account 2'),
        'name': fields.text('Description', required=True),
        'quantity': fields.float('Quantity', required=True),
        'uom_id': fields.many2one('product.uom', 'Unit of Measure',required=True),
        'price_unit': fields.float('Unit Price', required=True),
        'price_subtotal': fields.function(_amount_line, string='Sub Total', type="float",digits_compute= dp.get_precision('Account')),
        'car_license_plate': fields.text('Nummerplaat', required=True),
        'car_rent_price': fields.float('Prijs', required=True),
        'car_number_of_units': fields.integer('Aantal'),
    }

As you can see I have multiple fields that are required, since they really need to be filled in. Most of these fields are used in the many2one product_id and four of these fields are for the many2one car_id.
As you can see in the XML view:

 <div attrs="{'invisible': [('recurring_invoices','=',False)]}">
                        <field name="recurring_invoice_line_ids" >
                            <tree string="Account Analytic Lines" editable="bottom">
                                <field name="product_id" on_change="product_id_change(product_id, uom_id, quantity, False, parent.partner_id, False, parent.pricelist_id, parent.company_id)"/>
                                <field name="name"  attrs="{'required':[('product_id', '=', True)]}"/>
                                <field name="quantity" attrs="{'required':[('product_id', '=', True)]}"/>
                                <field name="uom_id" attrs="{'required':[('product_id', '=', True)]}"/>
                                <field name="price_unit" attrs="{'required':[('product_id', '=', True)]}"/>
                                <field name="price_subtotal"/>
                            </tree>
                        </field>
                        <field name="recurring_invoice_car_line_ids">
				<tree string="Account Analytic Lines" editable="bottom">
					<field name="car_id" on_change="car_id_change(car_id, car_license_plate, car_rent_price)"/>
					<field name="car_license_plate" attrs="{'required':[('car_id', '=', True)]}"/>
					<field name="car_rent_price" attrs="{'required':[('car_id', '=', True)]}"/>
					<field name="car_number_of_units" attrs="{'required':[('car_id', '=', True)]}"/>
				</tree>
			</field> 
</div>

My question is now how can I guarantee that the fields must be filled in, depending on the requirements?
All the samples:
A user wants to add one product to the many2one product_id but nothing to car_id. 
=> Only the fields for the many2one product are required (as specified in the model).
A user wants to add one car to the many2one car_id but nothing to product_id
=> Only the fields for the many2one car_id are required (as specified in the model).
A user wants to add both product(s) and car(s) to both many2one relations  (as specified in the model).
=> All fields on the many2one product_id should be filled in and all fields on the many2one car_id should be filled in.

How exactly should I do this? I know I need domains but I don't know how to only force the user to fill in fields when something in the many2one is filled in..
I've tried a few ways but I keep getting this dialog


1

Yenthe

--Yenthe--
6696
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 4/3/15, 8:16 AM

The problem is in the domains logic and where they're applied.
You can make a field in the many2one required by doing something along these lines:

 <field name="recurring_invoice_car_line_ids">
				<tree string="Account Analytic Lines" editable="bottom">
					<field name="car_id" on_change="car_id_change(car_id, car_license_plate, car_rent_price)" attrs="{'required':[('car_id', '!=', '')]}"/>
					<field name="car_license_plate"/> <field name="car_rent_price" attrs="{'required':[('car_id', '!=', '')]}"/>
					<field name="car_number_of_units" attrs="{'required':[('car_id', '!=', '')]}"/>
				</tree>
			</field>
This means you only need to fill in the fields car_number_of_units and car_rent_price when there is an id set in the many2one. If there are no records it is not required. The fields that have no domain are not required to be filled in at any time.

attrs="{'required':[('car_id', '!=', '')]}" 

This statement literally says that the field should only be required when there is some id on the field car_id. When there is nothing it shouldn't be required.

1

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 4/3/15, 5:14 AM

You replace '==' by '=' and verify solutions again.

Thanks for the reply Zbik! I've changed this but no succes. I've updated my whole question with all the new code but there is still something wrong. Any idea?

Yenthe
on 4/3/15, 7:43 AM

In _column definition you have many ... required=True, remove them if possible.

zbik
on 4/3/15, 8:02 AM

Since your answer did help me I've upvoted it! I've made an own answer on this topic too with the solution and the explenation. Who knows somebody will need it one day! Thanks Zbik!

Yenthe
on 4/3/15, 8:17 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/2/15, 10:24 AM
Seen: 710 times
Last updated: 4/3/15, 8:18 AM