Skip to Content
Menu
This question has been flagged
2 Replies
1064 Views

I add a customized class into sale.py, but it never work irrespective of whether i upgrade apps, restart odoo or unstall and then install.

In the Technical/Databasee structure/Models/Sales Order/Fields, I cant find out  my customized Field( amount_in_words and amount_in_figures ) . 

So i am wonder how to make it work?

Here's my code.(update)

part 1:

class SaleOrder(models.Model):
_name = "sale.order"
_inherit = ['portal.mixin', 'mail.thread', 'mail.activity.mixin', 'utm.mixin']
_description = "Sales Order"
_order = 'date_order desc, id desc'
_check_company_auto = True

def num2chn(self):
numchar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
pr = ['圆', '拾', '佰', '仟', '萬', '拾']
total = int(self.amount_total)
length = len(str(total))
chn = ''
for i in str(total):
length -= 1
chn = chn + ('%s%s' % (numchar[int(i)], pr[length]))
return chn

part2:

amount_chn = fields.Text(string='大写金额', readonly=True)
name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, states={'draft': [('readonly', False)]}, index=True, default=lambda self: _('New'))
origin = fields.Char(string='Source Document', help="Reference of the document that generated this sales order request.")
client_order_ref = fields.Char(string='Customer Reference', copy=False)


part 3

@api.model
def create(self, vals):
if vals.get('name', _('New')) == _('New'):
 seq_date = None
  .........
 vals['pricelist_id'] = vals.setdefault('pricelist_id', partner.property_product_pricelist and partner.property_product_pricelist.id)

vals['amount_chn'] = self.num2chn(vals['amount_total']) #just add one row code here.

result = super(SaleOrder, self).create(vals)
return result

Avatar
Discard
Best Answer

Are you adding the field in standard sale module and you want to add your field in sale.order object?
If yes, then add your field under sale.order object but in your code I just see "class project_parm(models.Model):"


Avatar
Discard
Best Answer

Hello Cris,

Regarding with your issue, odoo doesn't know where to put the field that you just have define in your class. To sold this, you need to define a _name/(table name in postgresql) for that class:

class project_parm(models.Model):
_name = 'example.model' #table name in postgresql

amount_chn = fields.Char(string='大写金额', readonly=True)

def num2chn(self,total):
numchar = ['', '', '', '', '', '', '', '', '', '']
pr = ['', '', '', '', '', '']
lengh = len(str(total))
for i in str(total):
lengh -= 1
print('%s%s' % (numchar[int(i)], pr[lengh]), end='')
@api.model
def create(self, vals):
vals['amount_chn'] = self.num2chn(vals['amount_toal'])
return super(project_parm, self).create(vals)

Now if your objective is to extend the functionality of the sales order module, I highly advice you to create a separate module and inherit the model instead rather than just directly code it inside the sales.py as it might be a bit troublesome if you're debugging your code or you might accidentally change something inside the sales.py that may lead to some serious code breakage.

Here's the code for extending the sales order model:

class project_parm(models.Model):
_inherit = 'sale.order'

amount_chn = fields.Char(string='大写金额', readonly=True)

def num2chn(self,total):
numchar = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
pr = ['圆', '拾', '佰', '仟', '萬', '拾']
lengh = len(str(total))
for i in str(total):
lengh -= 1
print('%s%s' % (numchar[int(i)], pr[lengh]), end='')
@api.model
def create(self, vals):
vals['amount_chn'] = self.num2chn(vals['amount_toal'])
return super(project_parm, self).create(vals)

Hope this helps you.

Avatar
Discard