This related field working fine in odoo 9, but not in odoo 10. The field customer_id not updated when I create a new record with nomor_hp_id.
nomor_hp_id = fields.Many2one(
string='Nomor hp',
comodel_name='nomor.hp',
ondelete='cascade',
)
customer_id = fields.Many2one(
string='Customer',
related='nomor_hp_id.customer_id',
ondelete='cascade',
store=True,
readonly=True,
)
Try to start new database but the result still not updated.
You have to give comodel name inside Many2one field either it is normal Many2one or related Many2one. Please have a look at below code. You will get your answer.
nomor_hp_id = fields.Many2one(string='Nomor hp', comodel_name='nomor.hp',ondelete='cascade',)
customer_id = fields.Many2one(string='Customer', comodel_name='res.partner', related='nomor_hp_id.customer_id', ondelete='cascade',readonly=True,)
You have to define the reference of which table. Here customer_id is the reference field of the "res_partner" table or "res.partner" model.
This way works fine for me.
customer_id = fields.Many2one(
string='Customer',
related='nomor_hp_id.customer_id',
store=True,
)
Related
I'm trying to develop simple library app with Odoo but I get an error with manytomany relationship.
Here are classes:
class Book(models.Model):
_name = "library.book"
publisher_id = fields.Many2one(comodel_name="library.book.partner", string="Publisher")
author_ids = fields.Many2many(comodel_name="library.book.partner", relation='library_book_profile_partner_authors', column1='book_id', column2='partner_id', string="Authors")
class Partner(models.Model):
_name = "library.book.partner"
_inherit = "res.partner"
published_book_ids = fields.One2many("library.book", "publisher_id", string="Published Books")
book_ids = fields.Many2many("library.book", 'partner_books', 'book_id', 'partner_id', string="Authored Books")
This is the error I always get when installing the app
TypeError: Many2many fields library.book.partner.channel_ids and res.partner.channel_ids use the same table and columns
Someone can help to solve this please ?
It's the _inherit = "res.partner". Odoo is inheriting the model with a new model name which means it's creating a new table and copying all fields. For channel_ids it's trying to create the many2many "join" table, but with the same name as in res.partner. That happens because the table name is directly defined in the field (mail_channel_partner).
channel_ids = fields.Many2many('mail.channel', 'mail_channel_partner',
'partner_id', 'channel_id', string='Channels', copy=False)
So to solve the problem you have to "redefine" channel_ids on your new model again and change the table name, for example like:
channel_ids = fields.Many2many(relation='mail_channel_library_book_partner')
I have two models reservation(inherit from sale.order) and places . I want to make a one2many field in the places model so when a reservation(sale.order) is confirmed, the new customer who reserved the place is added to this field
here is my code
model reservation
from odoo import fields, models,api,_
from odoo.exceptions import Validation-error
class Customers2(models.Model):
_inherit='sale.order'
client=fields.Many2one('res.partner',string='client')
secure_place=fields.Many2one(comodel_name='product.template',string='Secure place')
guests=fields.Integer(string='guests')
hotel_reser=fields.Many2one('product.template')
start_date=fields.Datetime(string='start date')
end_date=fields.Datetime(string='end date')
reserv_price=fields.Monetary(string='Price')
currency_id = fields.Many2one(comodel_name='res.currency',string='Currency')
reserv_status = fields.Selection(
[('C', 'Confirmed'), ('D', 'Draft')],
string='Reservation type')
model places
from odoo import fields , models,api
class Place(models.Model):
_inherit='product.template'
hotel=fields.Selection([('H', 'Habit'),('Y','Yasmine'),('M','movenpick')],string='Hotel')
type_of_room=fields.Selection([('S', 'spa'),('M','meeting room'),('N','Nightclub')],string='Room')
reserv_persons=fields.One2many('sale.order','hotel_reser',string='clients reserved',compute='_compute_reservations')
To add the new customer who reserved the place in the reserv_persons field, the related model should be res.partner and its type many2many.
You want to use a computed field (the value is computed when needed), so you can search for sale orders in the sale state to show all customers who reserved the place
Example:
from odoo import api, fields, models, _, Command
class Place(models.Model):
_inherit = 'product.template'
reserv_persons = fields.Many2many('res.partner', string='clients reserved', compute='_compute_reservations')
def _compute_reservations(self):
sale_order = self.env['sale.order']
for tmpl in self:
tmpl.reserv_persons = [Command.link(so.client.id) for so in sale_order.search(
[('secure_place', '=', tmpl.id), ('state', '=', 'sale')])]
The Command used above is a special command to manipulate the relation of One2many and Many2many
Hi I created the following model:
class PrescriptionsPrescriptions(models.Model):
_name = 'prescriptions.prescriptions'
name = fields.Many2one('res.users','Name', default=lambda self: self.env.user, readonly=True)
Date_entered = fields.Date(string='Date', default=fields.Date.today())
paper_prescriptions = fields.Selection([('yes', 'Yes'), ('no', 'No')], string='Paper Prescriptions?')
However I cannot get the _sql_constraints to work:
_sql_constraints = [('log_unique','unique(name,Date_entered)','You have already logged data for that date.')]
I'm trying to get it so that each person can log only one prescription per Date_entered. Would greatly appreciate any help. :)
Odoo will not be able to add the constraint because of a psycopg2.ProgrammingError
column "date_entered" named in key does not exist
You just need to rename it to date_entered and update the module.
First you can’t have capital letters as starting of your variable.
Second the only reason constraints don’t work after updating is when there is already data violating the constraints
I am using Odoo 10. I have a custom field call linear_units in Sales Order. I have make to order ticked and it creates an automatic Purchase order. I would like to include the field linear_units from Sales order to the purchase order. With below code I can select the Sales order but I cant figure out how to add a field.
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
sale_order_id = fields.Many2one(
'sale.order',
"Sale Order",
help="Reference to Sale Order")
The above code works for selecting a sales order in purchase order. I have a float field in Sales order called linear_units. I need this field to copy to purchase order. I tried below but does not work
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
linear_units2 = fields.Float("Linear Units")
#api.onchange('product_id','linear_units')
def _onchange_product_qty(self):
if self.product_id:
self.linear_units2 = self.sale.order.linear_units
you can add a related field in the purchase order for linear_units like below
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
sale_order_id = fields.Many2one('sale.order', "Sale Order", help="Reference to Sale Order")
linear_units = fields.Float(related='sale_order_id.linear_units')
It will fetch the related linear_units value from the selected sale_order_id
hope this helps!
What is the purpose of this field. Is it supposed to be on each order line or is it supposed to be on the sale order as a whole. With the setup you have, you two options: First
sale_order_lines = fields.One2many('sale.order.line', 'Sale Order Lines')
Then from there you can reference your order number and your linear units.
sale_order_id = fields.Many2one('sale.order', related='sale_order_lines.order_id', string='Sale Order')
linear_units2 = fields.Float(related='sale_order_lines.linear_units', string='Linear Units')
and Second:
sale_order_id = fields.Many2one('sale.order', string='Sale Order')
linear_units = fields.Float(related='sale_order_id.sale_order_lines.linear_units', string='Linear units')
Though I'm not entirely certain that the second option will work. If this is the same value on all order lines then I would suggest putting linear_units on sale.order, then if you need it on the order lines you can put a related field on the order lines and then your fields will look like below
class SaleOrder(model.Models):
_inherit='sale.order'
linear_units = fields.Float(string='Linear Units')
class SaleOrderLines(model.Models):
_inherit='sale.order.lines'
linear_units = fields.Float(related='order_id.linear_units', string='Linear Units', readonly=True)
class PurchaseOrder(models.Models):
_inherit='purchase.order'
sale_order_id = fields.Many2one('sale.order', string='Sale Order')
linear_units = fields.Float(related='sale_order_id.linear_units', string='Linear Units', readonly=True)
(I suggest putting the read only on your related fields because if they are changed on your inherited view it will change it for that sale order and all of its relations.)
I created a new model 'sale.order.category' in order to group Sale Order Lines in specific subcategories (allowing to display subtotals, etc.)
class SaleOrderCategory(models.Model):
_name = 'sale.order.category'
name = fields.Char('Name', required=True)
line_ids = fields.One2many('sale.order.line', 'category_id', 'Order Lines in this category')
order_id = fields.Many2one('sale.order', 'Order', required=True, readonly=True)
class SaleOrder(models.Model):
_name = 'sale.order'
_inherit = 'sale.order'
order_category_ids = fields.One2many('sale.order.category', 'order_id', 'Categories in this order', readonly=True, copy=True)
Just for info, here is my Order lines tree view modification to add the Category column :
<!-- adds a category column in the order lines list -->
<xpath expr="//field[#name='order_line']/tree/field[#name='name']" position="after">
<field name="category_id"/>
</xpath>
My question is : how can I automatically populate the order_id field with the current Sales Order ID when I create a new Category through the Order Lines Tree (inside a Sales Order) ?
Many thanks,
Max
Preliminary remark: your use case seems related to what the official sale_layout module does, so you might want to have a look at it before going any further. Perhaps you could extend it instead of starting from scratch.
Next, the most basic answer to your question is to pass a default value for the order_id field of your sale.order.category model when you create it from the view. You can do that by setting a context with an appropriate default value on the many2one field from which you will create the value:
<xpath expr="//field[#name='order_line']/tree/field[#name='name']" position="after">
<field name="category_id" context="{'default_order_id': parent.id}"/>
</xpath>
Your category_id field is defined on the sale.order.line tree view, so parent will dynamically refer to the parent record inside the web client interface, here the sale.order.
However this option will not work well:
When you're creating a new sales order, you will have to create your categories before the sales order is even saved, so there is no possible value for order_id yet. For this reason, you cannot make order_id required, and you will have to set its value again later, or you will need to save your orders before starting to add the categories.
You already have an order_lines one2many field in your sale.order.category model. The order_id field is redundant with the line_ids field, because all lines presumably belong to the same order.
A simple alternative would be to entirely omit the order_id field (use lines_id[0].order_id when you need it), or to replace it with a related field that will be automatically computed from the lines (it will take the value from the first order line):
order_id = fields.Many2one('sale.order', related='line_ids.order_id', readonly=True)
What you should do depends on your requirements, it's difficult to say based only on your question.