I want to store standard_price field of prodct.product table to use this field value in sql query.
Code is here:
# -*- coding: utf-8 -*-
from odoo import fields, models
from odoo.addons import decimal_precision as dp
class Product(models.Model):
_inherit = "product.product"
standard_price = fields.Float(
'Cost', company_dependent=True,
digits=dp.get_precision('Product Price'),
groups="base.group_user",store=True,
help = "Cost used for stock valuation in standard price and as a first price to set in average/fifo. "
"Also used as a base price for pricelists. "
"Expressed in the default unit of measure of the product.")
You can not store company_dependent field in a database. Because when the company is change then company_depedent field value is also changed.
This field data is stored in ir.property object in odoo which name is Company Properties in odoo.
Related
In product.template there is field default_code. Is it' possible to add sql_constraints that default code should be unique. Because this code doesn't work. Or do i need override default_code field in my costume module?
class ProductProduct(models.Model):
_inherit = 'product.template'
_sql_constraints = [
('code_uniq', 'unique (default_code)', "Default code already exists!"),
]
Please try with Python constrain may its useful for you :
import this lines in python file :
from openerp.exceptions import ValidationError
Any write this method in your class :
#api.constrains('default_code')
def _check_default_code(self):
code = self.search([('default_code','=',self.default_code)])
if len(code) > 1:
raise ValidationError(_("Duplicate Record"))
I would add the constraint on model product.product because that's where this information (product reference) really is used. But default_code on product.template will only work since Odoo V10. In Odoo V8 and V9 it was a unstored related field, so not in DB. So you have to add the constraint on model product.product.
class ProductProduct(models.Model):
_inherit = 'product.product'
_sql_constraints = [
('code_uniq', 'unique(default_code)', "Default code already exists!"),
]
Important to know: If the module, which sets up the constraint, is updated while the constraint will fail (e. g. the default_code actually twice in db), it won't create a sql constraint in db. So you have to clean up the data and update the module again or create the constraint in the db by yourself.
I have:
a) given product_template_id (i.e. id 100) and
b) a duplicated product_template_id (i.e. id 200) created using copy() method
copy() method copies only product.template model, so suppliers for that specific product are not copied.
I would like to duplicate all suppliers for that model, but now I am wondering which is the right way to do it in Odoo.
If I understood the model properly suppliers prices for a given product are stored in product_supplierinfo table, where each record that points to a given product_tmpl_id specifices a supplier price/qty for a given product_template.
Which would be the way in Odoo to search for all records that point to a given product_tmpl_id (i.e. 100), duplicate them changing product_tmpl_id to the new one (i.e. 200)?
Excerpt from the ORM Documentation:
copy (bool) -- whether the field value should be copied when the record is duplicated (default: True for normal fields, False for One2many and computed fields, including property fields and related fields)
The field you're referring to is seller_ids, whose field definition is below:
seller_ids = fields.One2many('product.supplierinfo', 'product_tmpl_id', 'Vendors')
The copy attribute is not explicitly defined, so it is False by default (as explained in the documentation above). If you want this field to copy along with the other values during the standard product "Duplicate" (copy method), you can do this:
class ProductTemplate(models.Model):
_inherit = 'product.template'
# This only changes the copy attribute of the existing seller_ids field.
# All other attributes (string, comodel_name, etc.) remain as they are defined in core.
seller_ids = fields.One2many(copy=True)
Alternatively
If you want to only have the field copied sometimes, you can extend the copy method to look for a specific context value and only copy based on that.
# This may take some tweaking, but here's the general idea
#api.multi
def copy(self, vals):
new_product = super(YourClass, self).copy(vals)
if vals.get('copy_sellers'):
new_product.seller_ids = self.seller_ids.copy({'product_id': new_product.id})
return new_product
# Whatever you have calling the copy method will need to include copy_sellers in vals
vals.update({'copy_sellers': True})
product.copy(vals)
I have installed module Sale Sourced by Line
by Camptocamp, Eficent, SerpentCS, Odoo Community Association (OCA) for Odoo 9.0.
The module creates a new many2one field as the code bellow:
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
warehouse_id = fields.Many2one(
'stock.warehouse',
'Source Warehouse',
readonly=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
help="If a source warehouse is selected, "
"it will be used to define the route. "
"Otherwise, it will get the warehouse of "
"the sale order")
Now i would like to access the value warehouse_id on account_invoice_report qweb. Please tell me what are posible solutions for my purpose? Thank for your time,
In account.invoice.line one field available sale_line_ids, based on that you can search warehouse_id from sale.order.line.
invoice.invoice_line_ids.mapped('sale_line_ids').mapped('warehouse_id')
This may help you.
I have a selection field in the res.partner model which is employmentstatus and the options there are employed
or unemployed. I want another field employmenttype have the attribute required=True if the employmentstatus='employed'
or required=False if the employmentstatus='unemployed'. The field right now sets required to True whether Partner is
employed or not (See attached image here).
Here is my code:
from openerp.osv import osv, fields
from openerp import tools
class custom_fields_partner(osv.Model):
_inherit = 'res.partner'
_columns = {
'employmentstatus' : fields.selection([
('employed','Employed'),
('unemployed','Unemployed')
],'Employment status', required=True, default='unemployed'),
'employmenttype' : fields.selection([
('0','Public'),
('1','Private'),
('2','Mission')],'Nature of employment', required="fieldproperty"),
}
#api.one
def fieldproperty(self):
if self.employmentstatus == 'employed':
return True
else:
return False
The required attribute is expected to be stored in the database and is not meant to be calculated on the fly. Best bet is to do it client side. If you look in the model ir.model.fields you will notice the the required field is stored in the db and is not meant to be computed.
In your xml use the attrs attribute. Here is an example.
<field name="field_name" attrs="{'required':[('other_field','=','other_value')]}"/>
So in this example the field called field_name is required only if the field other_field has a value of other_value but your can create a domain criteria that is more complex or less complex depending on your needs.
The field other_field mush be present in your view in order for this to work because the evaluation takes place client side. If you need to include a field for evaluation but do not want to display it you can make it invisible. Like this.
<field name="other_field" invisible="1"/>
I'm trying out Django/Mezzanine and if I have a custom user profile as such:
class UserProfile(models.Model):
user = models.OneToOneField("auth.User")
street_address1 = models.CharField(max_length=100)
street_address2 = models.CharField(max_length=100)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=32)
country = models.CharField(max_length=2)
phone = models.CharField(max_length=15)
Mezzanine creates a sign up form at account/signup/ and I would like to modify the Country field to have a drop down list of countries from a table or xml file. The foreign key is a two character field.
How should go about doing this? Do I create a model form or try to extend the right template (tried looking at accounts\templates\account_form.html but don't think it is there?
I believe if you defined a "choices" arg for the field, it'll do just that:
https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
A quick Google search will probably also reveal some pre-built packages for country lists.