Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
4749 Vistas

Hello,


My class purchase.order contain the following object :

new_line = {
'order_id': False,
'product_id': line.product.id,
'name': line.product.name,
'product_qty': line.quantity,
'date_planned': i.date_to,
'product_uom': 1,
'price_unit': line.price,
'history_field': [
('first',
str(line.quantity) + ' / ' + str((good_date + datetime.timedelta(hours=2)).strftime("%x")))
]
}

Then in purchase.order.line class I want to display into a selection field the object history field :

history_field = fields.Selection([], string='Historique')


I can retrieve it, I mean when I do a print it shows good values but I've got this error :

  File "/odoo/odoo-server/openerp/fields.py", line 1545, in convert_to_cache
    raise ValueError("Wrong value for %s: %r" % (self, value))
ValueError: Wrong value for purchase.order.line.history_field: [('first', '7.0 / 15/10/2020')]

I can see good values as inside my print in this error, but it still doesn't work.

Thanks
Avatar
Descartar
Mejor respuesta

you need to update the history_field field in the purchase.order.line model to match the format of the history_field value you are trying to set.

If you want to store multiple values in the history_field, you should use a One2many or Many2many field instead of a Selection field. Here's how you can do it:

  1. Define a new model for history_field:

class PurchaseOrderLineHistory(models.Model):

_name = 'purchase.order.line.history'

_description = 'Purchase Order Line History'


order_line_id = fields.Many2one('purchase.order.line', string='Order Line')

first = fields.Char(string='First')

# Add other fields as needed


  1. Update the history_field field in the purchase.order.line model to use the newly created model:


class PurchaseOrderLine(models.Model):

_inherit = 'purchase.order.line'


history_field = fields.One2many('purchase.order.line.history', 'order_line_id', string='Historique')


  1. Modify the creation of the new_line dictionary to use the new model:


history_vals = {

'first': str(line.quantity) + ' / ' + str((good_date + datetime.timedelta(hours=2)).strftime("%x"))

}


new_line = {

'order_id': False,

'product_id': line.product.id,

'name': line.product.name,

'product_qty': line.quantity,

'date_planned': i.date_to,

'product_uom': 1,

'price_unit': line.price,

'history_field': [(0, 0, history_vals)],

}


With this approach, the history_field in the purchase.order.line model will be a One2many field that can store multiple values, each represented by a record in the purchase.order.line.history model.

Avatar
Descartar
Mejor respuesta

Hi,

Although the history_field attribute of the purchase.order.line class is designed as a Selection field, which can only accept a single value from a preset list of options, it appears that you are attempting to store a list of tuples there. You are experiencing the "wrong value" error as a result.

Before you add the data to the history_field, you will need to prepare it properly to solve this problem. You should provide a list of tuples containing appropriate options for the field since the history_field is a selection field. The value to be stored and the display label should be present in each tuple.
from datetime import datetime, timedelta

# Assuming you have 'good_date' and 'line' defined somewhere

history_data = str(line.quantity) + ' / ' + str((good_date + timedelta(hours=2)).strftime("%x"))
history_field = str([(history_data, history_data)])

# Assign the history_field as a string to the selection field
history_field = fields.Selection([], string='History')

# Now set the value of the history_field
history_field = history_data

Please note that this method stores the history data in the selected field as a string. Use a JSON field or another appropriate data format if you may subsequently need to view or modify specific historical entries.

Hope it helps

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
dic 17
16689
0
abr 15
3816
1
mar 15
9369
1
dic 24
1395
2
sept 23
12200