Skip to Content
Menu
This question has been flagged
3143 Views


My code so far:

import re

template=My name is [name] and on [date] I have to give [amount] to my friend.

placeholder = re.compile('(\[([a-z]+)\])')

find_placeholder = placeholder.findall(template.text)# get the template

for tgt in tgt_ids: #for each target(i have many),get the name,date,amount

    name = self.pool.get(model).browse(cr, uid, tgt).name # the model is res.partner

    date = self.pool.get(model).browse(cr, uid, tgt).date

    amount = self.pool.get(model).browse(cr, uid, tgt).credit_limit

val = {'val_name': name,

    'val_date': date,

    'val_amount': amount,

}

message = template.text

for placeholder, key in find_placeholders:

    message = message.replace(placeholder, val.get('val_' + key, placeholder))

    print message

This is working right now, but if i want to get data from another data source(maybe external), probably the fields will have a different name.

For example - The key `name` might be changed to `first_name` or `date` to `payment_date`.

I want the code to be more flexible and to choose the right field every time. Is there any way to do this? Or do I have to change my code every time I change the source?Any ideas?

Avatar
Discard