Sorry, I don’t understand the ideology of the system.
I developed a module with a table that holds some items that are owned by my clients.
My company sells services. These services are related to items (so, actually items — are serviceable items own by clients.)
Hence, the invoice should look like:
item A1 - service B $10
item A2 - service B $10
item A3 - service C $10
To pay: $30
Actually, Items connected to parties (clients) — so I know which party has which item.
To what class (in what module) I should connect One2Many/Many2One fields in my ‘items’ module to be able to add such strings to invoices??
If that should be a Many2Many field? I think so because some items could be added to various invoices while the invoice could present several items in it. Am I correct?
How it would look like?
My guess:
class item(ModelSQL, ModelView):
'Items'
__name__ = 'my_module.item'
item_name = fields.Char(...
sales = fields.Many2Many(
'my_module.item-sale.sale', 'item_name', 'origin',
"Items",
help="The categories that the product is in.\n"
"Used to group similar products together.")
Decided to share where I came to. Goal: I need to indicate what items go under the service that is invoiced (the company sells services to Items that are owned by clients)
I started with adding My_module to the Origin field in Sales, but I don’t like it (there is only one item that could be connected). And initially, I forgot to add override initialization to __init__.py:
def register():
Pool.register(
my_module.Sale,
that is why I didn’t find my module in the list of origins at the beginning. Just a note for future explorers
———
Next. I didn’t find that is good. It is not the ‘origin’ and the only one item from my module is on the list. I need many.
Eventually, I decided to forgo this way and switched to a more elaborate method to connect Service to Items.
Hence, I decided to use Many2Many fields.
from trytond.pool import PoolMeta
# ——— Add to original class 'Sale' sale.sale my field
class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
items = fields.Many2Many(
relation_name = 'sale.sale-my_module.item',
origin = 'sale', # because it is the Sale class
target = 'item',
string='Related sales')
#———— Connection class,
# builds a symmetrical behaviuor, mirroring One2Many
# in both directions
class ItemServiceSales(ModelSQL):
'Items - Sales relation'
__name__ = 'sale.sale-my_module.item'
sale = fields.Many2One(
'sale.sale',
'Sales', # We are connected to ALL items (rows) in Sales class
required=True,
ondelete='CASCADE')
item = fields.Many2One(
'my_module.item',
'Items', # We are connected to ALL items (rows) in that class
required=True,
ondelete='RESTRICT')
# —————— My class of my module
class Item(ModelSQL, ModelView):
'Item'
__name__ = 'my_module.item'
item_sales = fields.Many2Many(
'sale.sale-my_module.item',
'item', # Origin is the 'item' because we are in the Item class
'sale',
string='Sales for this item')
Also I wraped fields sales / items in <notebook> <page> tags both in sale_form.xml and items_form.xml, created sale.xml with links to that form in the root directory of my module (and added ‘sale’ to dependences & a file sale.xml in tryton.cfg)
Now, I have my Items in the Sales module and may connect items (many of them) to any order.