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.

1

How can I show a warning message?

By
Juan Formoso
on 11/18/14, 6:18 AM 7,433 views

I added a function to a model, to set the default value of a field:

_defaults = {
    'template_id': _my_function,
}

In that function, I check some conditions and return the value. In one of those conditions, I have to show a warning message before returning the value. How to do this without stopping the normal execution of the program?

For example, the option warning does this:

'warning': {
    'title': _('Warning'),
    'message': _('My warning message.')
}

But it must be used in a return, and in this case, because of the function is called from defaults, it can't return a dictionary with a warning, but the value of the template_id.

The orm/osv exceptions make the following window a blank popup, I only want to notify some aspect to the user and then leave them working as if nothing had happened. In summary, how to show a simple info message?

Does anyone know it, please? 

By default this would not be possible. I have thought about the same thing before, so I will watch this issue closely for a fix of some sort.

Ludo - Neobis
on 11/18/14, 6:42 AM

By the way: A very dirty hack would be fire up a wizard which can have a cancel button as well as a regular "ok" button to continue with the flow.

Ludo - Neobis
on 11/18/14, 6:43 AM

Thank you Ludo! If I tried to do the hack you tell me, how can I fire up a wizard inside a function which is called from _defaults? I mean, the only way I know to fire up a wizard in python is through the return, so if I do that, it will generate an error because I'm not returning a right default value for the field, but a form.

Juan Formoso
on 11/18/14, 7:00 AM

Juan, the problem is that when _defaults is called, it does not have to be from the UI. It can be from a cron job, etc. So, in a way, there is no guarantee you have a UI when _defaults is called. Is there any way that you can default the value of the field from another way that is guaranteed have UI? One example is the on_change which already has the warning mechanism.

Ivan
on 11/18/14, 7:05 AM

The field I am trying to load its default value is a selection field. Do you mean this? Adding a new variable to the context in the function called from _defaults, and modifying its onchange method to show there the warning depending on the new variable added to the context?

Juan Formoso
on 11/18/14, 7:30 AM

Juan, what I mean is that _defaults can be called even if it is not triggered from a UI component. _defaults will be called if the copy_data() or create() method is called. So, if you have a cron job or workflow that creates that model, it will call _defaults. In those situation the warning cannot be prompted. So, I think you need to find another way to defaulting the value of the field this if you insist on prompting the warning. I'm not sure what will work until you can describe how you're going to use template_id field and what conditions are you checking.

Ivan
on 11/18/14, 7:57 AM

John Doe is absolutely right here. The defaults can be called from everywhere in the code. If you want something specifically for the GUI, consider using on_change methods on your fields to give the desired effects.

Ludo - Neobis
on 11/18/14, 8:02 AM

Ok. Thank you both of you!

Juan Formoso
on 11/18/14, 9:22 AM
3
Lucio
On 11/18/14, 9:49 AM

I would recommend doing an onchange function on the field template_id and check the value returned there. 
The function for the onchange will be called for the default value and there you will be able to return the desired warning message.

I do not know if you are working on V7 or V8, for simplicity i will state the solution for v8 you can just do something like:

@api.onchange('template_id')
@api.multi
def onchange_template_id(self):
    res = {}
    if <do your checks here>:
        res = {'warning': {
            'title': _('Warning'),
            'message': _('My warning message.')
        }
    if res:
        return res

Hope this helps!

 

Hi! I'm working on v7. I'm going to try this right now, and I'll comment you the result. Thank you!

Juan Formoso
on 11/20/14, 4:43 AM

That's great Lucio! However, if I update the context in _my_function (called from _defaults) to add a variable to check in onchange, it doesn't appear here, and I can't check if I have to show the warning or not. I need to pass that variable from _defaults to the onchange.

Juan Formoso
on 11/20/14, 5:19 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

3 follower(s)

Stats

Asked: 11/18/14, 6:18 AM
Seen: 7433 times
Last updated: 2/25/16, 10:57 AM