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.

2

How to use same field more than once in a form?

By
thenon
on 4/15/13, 3:57 PM 6,350 views

What would the view xml look like for a module that needed to use the same field more than once?

Explanatory example: you want to allow a phone number to be editable (like normal),

< field name="phone" />

but also want to do something like:

< field name="phone" widget="web_skypeButton" />

elsewhere on the page.

Whenever I include both, neither gets the value. Is there a better way of doing this?

A related field from the object to itself?

thenon
on 4/16/13, 8:29 AM
5

Francesco OpenCode

--Francesco OpenCode--
3608
| 5 7 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 4/16/13, 8:45 AM

You must use a function field for this:

class your_class(osv.osv):

    _inherit = "your.class"

    def _your_function(self, cr, uid, ids, name, arg, context=None):
        res = {}
        for id in ids:
            record = self.browse(cr, uid, id)
            res.update({id:record.phone})
        return res

    _columns = {
        'your_field' : fields.function(_your_function, type='char', size=32, method=True, store=False, multi=False),
        }

your_class()

and this is your view:

<record id="your_view" model="ir.ui.view">
            <field name="name">your.view.form</field>
            <field name="model">your.class</field>
            <field name="inherit_id" ref="original_module.original_form_view"/>
            <field name="arch" type="xml">
                <field name="phone" position="after">
                        <field name="your_field"/>
                </field>
            </field>
        </record>
  Thanks for that.   By record.phone I assume you meant record.your_field?

If so, I'm still confused by what the view would look like. This doesn't work for example:

   &lt;field name="arch" type="xml"&gt;
            &lt;field name="email" position="after"&gt;
                &lt;field name="your_field" string="Short code"/&gt;
            &lt;/field&gt;

            &lt;field name="your_field" position="after"&gt;
                &lt;field name="_your_function" string="Short code2"/&gt;
            &lt;/field&gt;
  &lt;/field&gt;


  and neither does:
thenon
on 4/16/13, 8:58 AM
         &lt;field name="arch" type="xml"&gt;
            &lt;field name="email" position="after"&gt;
                &lt;field name="your_field" string="Short code"/&gt;
                &lt;field name="_your_function" string="Short code2"/&gt;
            &lt;/field&gt;
  &lt;/field&gt;
thenon
on 4/16/13, 8:59 AM

I edit the answer with right situation

Francesco OpenCode
on 4/16/13, 9:04 AM

Thanks Francesco, but that view only contains one instance of the field, not two?

thenon
on 4/16/13, 9:06 AM

two. Phone is the original field and Your_Field is the second instance

Francesco OpenCode
on 4/16/13, 11:01 AM

In my instance phone is NOT already on the view - I'm trying to display two fields - one as normal edit, one as a widget (see my sample code) . I used phone as an easy to understand example to demonstrate the use case, but I need to declare both. The first field is actually the edit field. The second is going to be rendered by a widget and display something different with that field.

thenon
on 4/16/13, 11:15 AM

I'm sorry but I can't explain in 500 charaters how to create a view for openerp module. All you need to know is that with the field function you can duplicate your original field content. I do assume that you already know how to create a view for an openerp module.

Francesco OpenCode
on 4/16/13, 11:18 AM

Perhaps you could advise if this code is correct? http://pastebin.com/5TJx3d55

thenon
on 4/16/13, 11:31 AM

line 14 must be: res.update({id:record.mycol})

Francesco OpenCode
on 4/16/13, 11:38 AM

That code is not quite correct (should be record.mycol) - but now have it working essentially in that both are being rendered ! Thank you. Now just need to work out how to include a xpath AND a normal "after" field at the same time...

thenon
on 4/16/13, 11:40 AM

And the answer for anyone else looking, is to wrap in a <data> element.

thenon
on 4/16/13, 11:45 AM
2

Ray Carnes

--Ray Carnes--
15717
| 9 7 9
Greater Los Angeles, United States
--Ray Carnes--

Senior Odoo Analyst

OpenERP 6.1, 7.0 and Odoo 8.0, 9.0 (Since 2012)

Completed Functional and Technical Training.

Major Skills:

  • Needs Discovery and Requirements Analysis;

  • Function and Technical Specifications;

  • Project Planning;

  • Prototyping and Proof of concepts;

  • Data migration;

  • Configuration & Customization (UI and modules);

  • Integration - data, business logic and service levels;

  • Training and Knowledge transfer;

  • Go Live support;

  • Help desk;

  • Version Migration.

I have over 20 years of experience empowering and enabling users with enterprise information systems that make a real and measurable difference in their ability to proactively manage their businesses and organizations. 

Ray Carnes
On 9/22/14, 2:07 AM

I think you probably need a related field. Every time I've seen people put the same field on a form more than once, things don't work properly.

It much easier and probably faster than using functional fields as other answers suggested.

    _columns = {
        'phone': fields.integer('Phone'),
        'phone_2': fields.related('phone', string='Phone'),
    }

2

Vasiliy Birukov

--Vasiliy Birukov--
3446
| 6 6 8
Russian Federation
--Vasiliy Birukov--

Help develop and implement any project on base Odoo.

Vasiliy Birukov
On 4/16/13, 8:17 AM

You can define two field:

  • first - what you need
  • second - functional field, that return value from first field

This code for python:

class phone_example(osv.osv):
    _name = 'phone.example'

    def _get_phone(self, cr, uid, ids, name, arg, context=None):
        res = {}
        for phone in self.browse(cr, uid, ids, context=context):
            res[phone.id] = phone.phone
        return res

    _columns = {
        'phone': fields.integer('Phone'),
        'phone_2': fields.function(_get_phone, method=True),
    }

Then you can use two field, that will show the same.

Note. Second field will show only data from DB. If you change first in form, second don't update while you don't save form. Use also onchange method for update second field realtime.

I'm very new to python - what would that second one look like in code please?

thenon
on 4/16/13, 8:35 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

2 follower(s)

Stats

Asked: 4/15/13, 3:57 PM
Seen: 6350 times
Last updated: 3/16/15, 8:10 AM