[Odoo][v15] Создайте ссылку One2Many на поле. Ссылка как обратное_имя.Python

Программы на Python
Anonymous
[Odoo][v15] Создайте ссылку One2Many на поле. Ссылка как обратное_имя.

Сообщение Anonymous »

У меня 2 модели:

Код: Выделить всё

class DiscountLineMixin(models.AbstractModel):

_name = "discount.line.mixin"
_description = "Discount Line Mixin"

product_discountlist_ids = fields.One2many(
comodel_name="discountlist.percent",
inverse_name="order_line_id",
string="Product discountlist item effective_percent",
)
и

Код: Выделить всё

class ProductDiscountPercent(models.Model):

_name = "discountlist.percent"
_description = "Discountlist rules discountlist per order line"

model_name = fields.Char()
record_id = fields.Integer()
order_line_id = fields.Reference(
selection="_select_target_model",
string="Order Line",
)
discountlist_item_id = fields.Many2one(
comodel_name="product.discountlist.item",
string="Discountlist item",
)
effective_percent = fields.Float(
"Effective Percentage Price",
)

@api.model
def _select_target_model(self):
models = self.env["ir.model"].search([])
return [(model.model, model.name) for model in models]

@api.model
def create(self, vals):
if "model_name" in vals and "record_id" in vals:
vals["order_line_id"] = f"{vals['model_name']},{vals['record_id']}"
return super(ProductDiscountPercent, self).create(
vals
)

def write(self, vals):
if "model_name" in vals or "record_id" in vals:
for record in self:
model_name = vals.get("model_name", record.model_name)
record_id = vals.get("record_id", record.record_id)
vals["order_line_id"] = f"{model_name},{record_id}"
return super(ProductDiscountPercent, self).write(vals)
Я хочу создать отношение one2many из абстрактной модели в обычную модель.
Но когда я создаю запись из абстрактной модели:

Код: Выделить всё

new_values = [
(0, 0, {
"model_name": self._name,
"record_id": self.id,
"discountlist_item_id": discountlist_item.id,
"effective_percent": effective_discount,
}),
]
self.write({"product_discountlist_ids": new_values})
или

Код: Выделить всё

discountlist_item_percent = self.env["discountlist.percent"].create({
"model_name": self._name,
"record_id": self.id,
"discountlist_item_id": discountlist_item.id,
"effective_percent": effective_discount
})
self.product_discountlist_ids = [(4, discountlist_item_percent.id)]
в обоих случаях у меня есть созданная запись Discountlist_item_percent, но добавление ссылки на product_discountlist_ids не работает.
В отладчике я есть:

Код: Выделить всё

self.product_discountlist_ids = discountlist.percent()
discountlist_item_percent = discountlist.percent(284,)
Я предполагаю, что проблема связана с использованием field.Reference для inverse_name в One2Many, но не знаю, как это исправить. Мне нужна ссылка, потому что эта абстрактная модель используется во многих других моделях.

Подробнее здесь: https://stackoverflow.com/questions/785 ... verse-name

Вернуться в «Python»