This question has been flagged
2 Replies
3794 Views

I have a function field which is defined like this:

def _get_field_value(self, cr, uid, ids, field_name, args, context=None):
    return ...

def _get_related_others(self, cr, uid, other_ids, context=None):
    return self.pool.get('this.model').search(cr, uid,
                         [('other_id','in',other_ids)], context=None)

'field': fields.function(_get_field_value, ...,
    store={'other.model': (_get_related_others, ['other_field'], 10)}),

The idea is that when the user creates a new resource in the other.model an update of 'field' is triggered. The update is triggered without any problems, but the _get_related_others can't retrieve the given other_ids from other.model. It gets passed a list containing one id (of a new other.model resource), but self.pool.get('other.model').search() returns no IDs.

I can see that the newly created other.model resource is not in the database at the time that _get_related_others is called, but isn't this just because it's in a transaction that's yet to be committed? And shouldn't the self.pool.get('other.model').search() call be working with the same transaction? And therefore have access to he newly created other.model resource?

Otherwise, please explain how/whether this is possible.

EDIT

Apologies, I abstracted my actual problem incorrectly. I'm aware that the aim is to return a list of IDs on this.model that need to be updated, but the problem is that I want to select resources from this.model which are related to the resource(s) on other.model that changed.

this.model has a field, 'other_id', that points to other.model. In my _get_related_others method I intend to select all the this.model resources where other_id is in the list of other_ids that changed. However, I find that:

self.search(cr, uid, [('other_id','in',other_ids)], context=None)

is wrong because self is actually a other.model object (I expected it to be this.model). And

self.pool.get('this.model').search(cr, uid, [('other_id','in',other_ids)], context=None)

(as I've edited to above to now read) doesn't work as it returns an empty list. It should be returning resources on this.model whose other_id field points to the newly created resource on other.model.

I hope that's a bit clearer?

Avatar
Discard

I am not sure what do you want to do, are you trying to look for the newly created id? Isn't that the id in other_ids?

But if you just created the other.model, how is it that any of the this.model is related with it? If, on creation, you set the relation through other.model in, lets say, field first_model, you can access to it whit something like self.browse(cr,ids[0],context).first_model

Author

Now, I have to apologise again. I've worked more on this today and found that the problem was very specifically to do with the actual models I was working with, and that my abstract version here would actually work fine. So I guess this question is either 'answered' or, better, 'asked in error'; not sure if there's a state for that.

Best Answer

You have to return the id(s) of current model from _get_related_others, not id(s) from other.model. Try something like that:

def _get_related_others(self, cr, uid, other_ids, context=None):
    result = set()
    for om in self.pool.get('other.model').browse(cr, uid, other_ids):
        result.add(om.id)
    return list(result)
Avatar
Discard
Best Answer

In the function _get_related_others the variable other_ids contains the Ids of the model other.model.

The job of this function is to return Ids of the current model (do not know how the model is called in your example).

But returning Ids of the model other.model is not correct!

I know that this is confusing, but once you got it it will be more clear :-)

Avatar
Discard