Correct way to make comparison based on other models fields - odoo

for line in invoice.invoice_line_ids:
if line.sale_line_ids:
if line.sale_line_ids.qty_avl_stock >= line.sale_line_ids.product_uom_qty:
term = _('At warehouse')
in a proforma invoice, I want to check sale_order_line fields that is related to invoice_line.
In account.invoice.line I found a relation to sale.order.line it's sale_line_ids field. my goal is to make some checking in related sale_order_line but is this a good way to do? or maybe I should do a search on sale.order.line model?
Because sale_line_ids field is many2many and probably there can be more than one record and if that is the case i will get an error here.
UPDATE for bounty
Basically, I need check order_line qty_avl_stock and product_uom_qantity that is related to invoice_line. But I don't know how is the best way to relate sale_order_line to invoice_line, or maybe there are other solutions?
class SaleOrerLine(self)
qty_avl_stock = fields.float("Quantity availible stock")

Related

OneToMany field in Odoo proper way to link two models

raise UserError(_("No inverse field %r found for %r") % (self.inverse_name, self.comodel_name))
odoo.exceptions.UserError: ("No inverse field None found for 'res.sector'", '' )
I'm trying to create a new model and having problems linking it to another model.
You probably declared a One2many field like following:
field_name = fields.One2many('res.sector')
You need to specify the inverse name (mandatory), the name of a Many2one field declared in res.sector that reference the model where you declared the One2many field.
As mentioned above, you need to specify an inverse name on the relation.
For example: (workcenter_id) is the inverse field
equipment_ids = fields.One2many('maintenance.equipment', 'workcenter_id')

How can I show one2many field of another model in my form view

I have a custom object "Car" with a one2many relationship to another object "Trips" which captures the various trips made by the car.I added the trips to the form view of the car so i can see all the trips made by each car. I created a custom many2one field in sale.order called "x_car" related to "Car" and added it to the sale.order form view. Now i want to add a one2many field too, which will show the trips of the car when it is selected in the sale.order form view. How can i achieve that?
I have already tried to create a one2many field in sale.order (trips,car_id) and set related to x_car.trips
I hoped it would pull all the records from Trips based on the selected x_car, but it does not return any data. I know a one2many relationship would typically be based on the object_id (in this case sale_order_id) but is there a way to base it on x_car?
You can use a computed many2many field for such purposes:
class SaleOrder(models.Model):
_inherit = "sale.order"
car_id = fields.Many2one(comodel_name="car")
trip_ids = fields.Many2many(
comodel_name="trip", string="Trips",
compute="_compute_trip_ids")
#api.multi
#api.depends('car_id')
def _compute_trip_ids(self):
for order in self:
order.trip_ids = order.car_id.trip_ids.ids
I bet your field names or a bit off of my example, but i always try to stick to the Odoo guidelines. Just substitute them with your names ;-)

How to relate fields in Odoo 11 CE

I added a custom field in "account.payment" model, I also added it in "account.move.line" model (aka: Journal Item).
The value of the custom field is entered from account.payment and since the journal items are generated from a payment creation I want to do the following:
If account.move.line.id is created by account.payment.id
let account.move.line.custom_field = account.payment.custom_field
I appreciate your help
You have to find in account.move.line that represent the relation(foreign_key) with account.payment, let says payment_id, this field allows you to access to account.payment model, and for your case you just need to do:
field_in_account_move_line = payment_id.field_in_account_payment
I hope this answer can be helpful for you.

How to move field in sale order to stock picking

I have a problem, I made two custom fields in
Sale.order
Stock.picking
How to do when the sale order is confirmed, the field in stock.picking also filled? and the data was picked up from the field at sale.order I've made before.
I'm using odoo 10
Thanks
Yeah, first, depend in your module of sale_stock, then, inherit the sales confirm button and search for the pickings associated:
#api.multi
def action_confirm(self):
result = super(SaleOrder, self).action_confirm()
for order in self:
order.picking_ids.write({'your_field_in_picking': order.your_field_in_sale})
return result
Put it in a class that inherits from sale.order

related attributes , related fields on inherited view odoo

what is related attribute , what it can be used for ? and how to add a related attribute .
I'm trying to add a related field for total amount.
Related Field
In such case we need to take the value from any other table but we already have the reference of that table in our model, in that case we can utilize one functionality with that we can add the multiple fields from the reference table by just having the only one reference field in our model.
One relational field (Many2one) of the source model is mandatory to
have in the destination model to create relational field.
Consider the company_currency_id field from the res.currency model is there in the destination model then you can get the current rate of that currency in your target model.
syntax: v7
_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}
Here,
company_currency_id => the field in the same model through which the new field will be relate,
rate_silent => is the field which you are going to relate with new field, means the field from source model,
relation => is the source model name,
type => is the datatype of the source field
Syntax: v8 and newer version
current_rate = fields.Float(string='Current Rate', related='company_currency_id.rate_silent', store=True, readonly=True)
Note :
Value is available in that field only after you save the record.
When you update value in your newly defined related field it
will be updated in source field as well, though it's always advisable
to set readonly in newly defined field.
In context of Odoo related field means, that its value will be
read from another table (related table) --> store=False
read from another table, but stored in the table its defined on --> store=True
Examples (Odoo V8):
sale.order currency_id: it is persisted in product_pricelist
stock_quant packaging_type_id: it is persisted in product_packaging and stock_quant. Everytime you change the value on product_packinging it will be updated on stock_quant, too, not vice versa.
**Related Field* is used when you wanted to pull a value from another model you can use related field on fields.
Here is an example for you.
order_id = fields.Many2one(comodel_name='sale.order', 'Sale Order')
order_amount = fields.Monetary(string='Order Amount',
store=True,
related='order_id.amount_total')
you must add a Many2one field in the model which relates to the model you want to access a field. in our case model is sale.order.
with the related kwarg you can relate field of the related model defined in the Many2one field. in our case order_id.
Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated.(Reference)
Hope this helps!