how to add a functional field in the domain's view

Asked by Houssine

Hi there,

i want to know if there is a way to add a functional field or even a function call(like workflows do) in a domain's view?

I'm trying to add an extra evaluation on existing ones for a dashboard.

Here is a sample of code :

<record id="purchase_waiting_product_owner" model="ir.actions.act_window">
            <field name="name">Purchase Order Waiting Approval</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">purchase.order</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="domain">[('date_order','&gt;',time.strftime('%Y-01-01')),('date_order','&lt;',time.strftime('%Y-12-31')), ('state','in',('wait','confirmed')), (belong_to_uid,'=',True)]</field>
            <field name="search_view_id" ref="purchase_order_tree_product_owner"/>
        </record>

Thanks for your science,
Houssine

Question information

Language:
English Edit question
Status:
Solved
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Solved by:
Quentin THEURET @Amaris
Solved:
Last query:
Last reply:
Revision history for this message
Best Quentin THEURET @Amaris (qtheuret) said :
#1

You could override the search method of purchase.order when a value in the context is set to True

In view :
<field name="context">{'current_year': True}</field>

In class definition:
def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
    if not context:
         context = {}

    if context.get('current_year', False):
        current_year = [('date_order','&gt;',time.strftime('%Y-01-01')),('date_order','&lt;',time.strftime('%Y-12-31'))]
        args.extend(current_year)

    return super(purchase_order, self).search(cr, uid, args, offset, limit, order, context=context, count=count)

Revision history for this message
Houssine (houssine-bakkali) said :
#2

Does this would work with a functional field defined as the following?

class purchase_order(osv.osv):
    _name='purchase.order'
    _inherit='purchase.order'

    def _belong_to_uid(self,cr,uid,ids,fieldnames,args,context=None):
        res={}
        for purchase_order in self.browse(cr,uid,ids,context=context):
            res[purchase_order.id] = False
            for order_line in purchase_order.order_line:
                product_owner = order_line.product_id.product_manager
                if(product_owner.id == uid):
                    res[purchase_order.id] = True
        return res

    _columns = {
            'belong_to_uid':fields.function( _belong_to_uid, method=True, type='boolean', string='Belong to product owner', store=False),
           }

Revision history for this message
Nhomar - Vauxoo (nhomar) said :
#3

Segmento

Nhomar HernandeZ desde mi cel linux based
El 03/04/2012 10:51, "Houssine" <email address hidden>
escribió:
>
> Question #192521 on OpenERP Server changed:
> https://answers.launchpad.net/openobject-server/+question/192521
>
> Status: Answered => Open
>
> Houssine is still having a problem:
> Does this would work with a functional field defined as the following?
>
> class purchase_order(osv.osv):
> _name='purchase.order'

You to inherit can not use _name just. _inherit

> _inherit='purchase.order'
>
> def _belong_to_uid(self,cr,uid,ids,fieldnames,args,context=None):
> res={}
> for purchase_order in self.browse(cr,uid,ids,context=context):
> res[purchase_order.id] = False
> for order_line in purchase_order.order_line:
> product_owner = order_line.product_id.product_manager
> if(product_owner.id == uid):
> res[purchase_order.id] = True
> return res
>
>
> _columns = {
> 'belong_to_uid':fields.function( _belong_to_uid, method=True,
type='boolean', string='Belong to product owner', store=False),
> }
>
> --
> You received this question notification because you are a member of
> OpenERP Committers, which is an answer contact for OpenERP Server.

Revision history for this message
Houssine (houssine-bakkali) said :
#4

Thanks Nohmar but I don't think it's the issue

Doing inheritance as I do is not wrong as far as I know
http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/object_inherit.html

Revision history for this message
Nhomar - Vauxoo (nhomar) said :
#5

You right i dont red well you question.

domains buil dinamically an sql sentence

if your field is store=false

it will not work.

Btw.

_name is not necesary

Segmento

Nhomar HernandeZ desde mi cel linux based
El 03/04/2012 11:51, "Houssine" <email address hidden>
escribió:

> Question #192521 on OpenERP Server changed:
> https://answers.launchpad.net/openobject-server/+question/192521
>
> Status: Answered => Open
>
> Houssine is still having a problem:
> Thanks Nohmar but I don't think it's the issue
>
> Doing inheritance as I do is not wrong as far as I know
>
> http://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/object_inherit.html
>
> --
> You received this question notification because you are a member of
> OpenERP Committers, which is an answer contact for OpenERP Server.
>

Revision history for this message
digitalsatori(Shine IT) (digitalsatori) said :
#6

As far as I can tell, the only purpose of the function field 'belong_to_uid' is to filter out purchase orders according to whether the current user is the product manager for all ordered goods in a PO.

For this particular case, you don't have to define this function field, use what Quentin THEURET suggested, put your filtering logic in the overided search method and use it with the context.

If you really want to define a function field, you may need to define a fnct_search method for your function field, eg:

'belong_to_uid':fields.function( _belong_to_uid, method=True, type='boolean', _fnct_search=XXXX,
string='Belong to product owner', store=False)

def XXX(self, cr, uid, obj, name, args)

and return an tuple argument for search, eg: [('id', 'in', [1,5,6])]

Revision history for this message
Quentin THEURET @Amaris (qtheuret) said :
#7

Houssine,

In fact, you can use an overrided search function for you 2 problems.

Revision history for this message
Houssine (houssine-bakkali) said :
#8

Ok thanks guys!

I'll try that!

Revision history for this message
Houssine (houssine-bakkali) said :
#9

Thanks Quentin THEURET, that solved my question.

Revision history for this message
Houssine (houssine-bakkali) said :
#10

Thanks also to DigitalSatori for his explainations

Revision history for this message
GEM (nimp3) said :
#11

Thanks to Quentin for this solution.
Bye