I have been trying the following code in openerp and I had been getting this error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: name - name]
In my py file, i had inherited one module
class hr_expense_expense(osv.osv):
_name = "hr.expense.expense"
_inherit = "hr.expense.expense"
_inherits = {'hr.employee':'first_approver'}
You don't give details, but I'm pretty sure the _inherits line should be removed. You probably made a confusion and actually want to add Many2one relation.
Related
How should I get all of the field names from the product.template model and store them in a selection field in a separate model?
Example
Thanks
I manage to fix the issue with the code below, I tried using self in the method get_fields but the following KeyError: product.template occurred so too bypass that I used request instead of self.
The selection field consists of [(product_template_field_name_1, product_template_field_name_string_1),..,(product_template_field_name_n, product_template_field_name_string_n)], n being the number of fields in the product.template model.
from odoo.http import request
def get_fields(self):
fields = [(field, request.env['product.template']._fields[field].string) for field in
request.env["product.template"]._fields]
return fields
field_name = fields.Selection(
selection=lambda self: self.get_fields(),
required=True)
I am trying to generate purchase order from manufacturing order.I have created many2many field for getting multiple products.I want to send multiple product ids to my custom function.I am able to send 1 value but sending more than one gives error as Expected singleton: product.template(4, 3).
from openerp import models,fields,api
class generate_purchase_order(models.Model):
_name = 'mrp_to_purchase_order'
product_id = fields.Many2many('product.template',string='Products',required=True)
#api.multi
def generate_purchase_order2(self):
for wizard in self:
mrp_obj = self.env['mrp.production']
mrp_obj.generate_purchase_order(wizard.product_id.id) #function call
return { 'type': 'ir.actions.act_window_close'}
mrp_custom.py,
from openerp import models,api
class mrp_production(models.Model):
_inherit = 'mrp.production'
#api.multi
def generate_purchase_order(self,product_id):
purchase_line_obj = self.env['purchase.order.line']
context = self._context
for order in self.browse(context['active_ids']):
for line in order.bom_id.bom_line_ids:
if line.product_id.id != product_id:#problem line
continue
#rest of code
singleton: product.template(4, 3)
This error means that code is expecting single record not recordset, so you must change code to allow recordset using or make ensureone with try-catch and avoid errors. Thats general information.
Now if you want to get multiple records from many2many its not problem at all, you must pass this many2many object only and then work with it.
After getting many2many object to work with every record from this recordset you must use for record in recordset:
Also wizard.product_id.id this is error!!! product_id is many2many so you must pass product_id or if you want to browse by yourself you must pass product_id.ids
I use delegation to inherit fields from sale_order in a custom module:
class EXAMPLE(osv.osv):
_name = 'EXAMPLECLASS'
_inherits = {
'sale.order': 'sale_order_id'}
I'm using the standard view for sale_order. Everything works fine, I can add and update order_lines within the sale_order. However, when I try to delete an order_line from the sale_order I get the following error:
Integrity Error
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[object with reference: order_id - order.id]
What am I missing? I think it has something to do with the fact that '_inherits' only inherits fields and not methods.
Any help on this matter would be greatly appreciated.
I really need to add an additional 'state' value on my Sale Order object. Since version 7.0, the 'sale_stock' module does exactly that already. When you try to do the same thing from your own module, your key,value just gets ignored. Is there any other alternative to achieve this?
As I found out, this seems to be an old time issue from two years ago as explained in this thread. A suggested workaround there was to do something like this:
_inherit = 'sale.order'
def __init__(self, pool, cr):
super(sale_order, self)._columns['state'].selection.append(('keyx', 'valuex'))
I found this approach logical, but it resulted in the following error:
`File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 2958, in _auto_init
self._field_create(cr, context=context)
File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 764, in _field_create
ir_model_fields_obj = self.pool.get('ir.model.fields')
AttributeError: 'sale.order' object has no attribute 'pool'`
Should this bug be reported at launchpad or is it an unintended use? What other possible solutions can you suggest? Thanks in advance.
try this
from openerp.osv import osv, fields
class sale_order(osv.osv):
_inherit = 'sale.order'
selection_list = [];#add your selection list here.
_columns = {
'state': fields.selection(selection_list,'State');#add necessary arguments
}
sale_order()
simply inherit the sale.order model and add your state field as it is which define in existing model , add the external state which you are required to add additionaly
for eg:
class sale_order(osv.osv)
_inherit ='sale.order'
_columns = {
'state': fields.selection([
('draft', 'Quotation'),
('waiting_date', 'Waiting Schedule'),
('manual', 'To Invoice'),
('progress', 'In Progress'),
('shipping_except', 'Shipping Exception'),
('invoice_except', 'Invoice Exception'),
('done', 'Done'),
('cancel', 'Cancelled'),
**('key','value')**,
This above would be your newly added selection value sequence dosen't matter.
], 'Order State', readonly=True, help="Gives the state of the quotation or sales order. \nThe exception state is automatically set when a cancel operation occurs in the invoice validation (Invoice Exception) or in the picking list process (Shipping Exception). \nThe 'Waiting Schedule' state is set when the invoice is confirmed but waiting for the scheduler to run on the order date.", select=True),
}
sale_order()
than abouve key value will be your additional selection field and you can set it any where in the squence as per your requirements.
having the same problem, I had a look at the thread, you noticed.
I guess that the problem comes from the fact that our modules and sale_stock are "in conflict" because they modify the same field ('state') in the sale.order object and are not depending of each other.
One solution is to modify your own module and adding 'sale_stock" in the 'depends' list of openerp.py :
depends : ['sale_stock',...]
You can see an example in this module which had another (key,value) in state field : http://bazaar.launchpad.net/~agaplan/agaplan-addons/7.0/files/head:/sale_double_validation/
Hope it helps
First, some background:
I have a Company model, a Project model and a Task model. A Project belongs to a company and a Task belongs_to a Project.
The Project model holds several attributes: company_id, date. These attributes uniquely identify a project
I am letting the users create a task by API by POSTing to a URL that contains the details necessary to identify the Project. For example:
POST /projects/<comnpany_name>/<date>/tasks/
In order to make life easier for the users, in case there is no project with the given details, I'd like to create the project on the fly by the given details, and then to create the task and assign it to the project.
...And my problem is:
When there is a problem to create the project, let's say that the company name is not valid, what is the right way to return the error message and communicate to the user?
I'll explain what I mean: I added a create_by_name_and_company_name method to the Project:
def self.create_by_name_and_company_name(name, company_name)
if company = Company.find_by_name(company_name)
project = Project.create(company_id: company.id,
name: name)
else # cannot create this project, trying to communicate the error
project = Project.new(name: name)
project.errors.add(:company, 'must have a valid name')
end
company
end
I was hoping that by returning an unsaved Company object, with errors set, will be a good way communicate the error (This is similar to how rails work when there's a validation error).
The problem is that when calling valid? on the company object, it removed the error I wrote there and adds the regular validation errors (in this case, company can't be blank).
And a bonus question...
And there is a conceptual problem as well: since I'm creating a model by providing parameters that are being used to create the actual attributes, they doesn't always map nicely to the errors[:attr] hash. In this case it is not so bad and I'm using the company field for the company name parameter, but I guess this can get messier when the parameters provided to the create method are less similar to the model attributes.
So what is the preferred approach to tackle that problem? Is there something basically wrong with that approach? if so, what is the preferred approach?
About overriding the default rails validation error message, you need to write your validation constraint like this:
validates_presence_of :name, :message => "must be a valid name"
I figure that it is best to avoid such nesting and stick to a shallower API.