How i can properly return a field data from another Many2one field(that has different data to return in get_rec_name))

Hi i would like to return a field data from another many2one field(that has different data to return in get_rec_name)) but i do no How i can do that.

this is the many2one field:

category =  fields.Function(fields.Many2One('proposition.data'
        ondelete='CASCADE', select=True), 'get_category')

this is function i made to let me return what i want(tradename from the class proposition.data) but it doesn’t work with me:

 @classmethod
    def get_category(self, category, name=None):
        Data = Pool().get('proposition.data')
        return Data.tradename

this is the class proposition.data:

class PropositionData(ModelSQL, ModelView):
    __name__ = 'proposition.data'
    _order_name = 'rec_name'

    description = fields.Char('Description', required=True)
    tradename = fields.Char('Tradename', translate=True)

    def get_rec_name(self, name):
        return self.description

this is the error displayed:

  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 875, in read
    row[fname] = getter_result[row['id']]
TypeError: 'Char' object is not subscriptable

How i can properly return a field data from another Many2one field(that has different data to return in get_rec_name))

Thanks in advance.

First you must decide if your getter is a class method or an instance method.
Second you use proposion.data instance to get the value of a field. But I do not know how your models are linked together.

But I do not know how your models are linked together.

*------>
okay about how they are linked :

I have a class A contain this code:

Class A:

category =  fields.Function(fields.Many2One('proposition.data'
        ondelete='CASCADE', select=True), 'get_category')

@classmethod
    def get_category(self, category, name=None):
        Data = Pool().get('proposition.data')
        return Data.tradename

and I have another class(PropositionData) that represent the many2one field:

class PropositionData(ModelSQL, ModelView):
    __name__ = 'proposition.data'
    _order_name = 'rec_name'

    description = fields.Char('Description', required=True)
    tradename = fields.Char('Tradename', translate=True)

    def get_rec_name(self, name):
        return self.description

Depending on what should I choose ? Thanks in advance.

Then instead of having a Function field of type Many2One you should create a new Function fields with the same type as the tradename fields. You will need a Many2One field that relates your record with the proposition.data model. Something like:

proposition = fields.Many2One('proposition.data', "Proposition")

and your function field should be like:

def get_tradename(self, name):
    if self.proposition: 
        return self.proposition.tradename

Hope it helps

Hi, Thanks for your reply,

This time it gives me this error:

in get_tradename
    return self.proposition.tradename
`AttributeError: 'Many2One' object has no attribute 'tradename'`

I

I guess you are using a classmethod instead of an instance one.

It will be great if you can share your full code so we can check that everything is right.