Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to read and write to product.template from custom module [Closed]

By
Abdullah Sofan
on 12/6/14, 4:16 AM 1,226 views

The Question has been closed for reason: duplicate question

by
Abdullah Sofan
on 12/06/2014 12:40:26

I have written this module which creates a course and at the same time creates a product in product.template with the same name as the course. When I create a course, the code works well and a course is created as well as a product. But when I try to "Edit" the course fee value, I get the error:

AccessError, The requested operation cannot be completed due to security restrictions. Please contact your system administrator.(Document type: product.template, Operation: read)

Also, When I change the value of the field course_name, I get this error:

DataError: invalid input syntax for integer: "Microsoft Course" LINE 1: ...=1,write_date=(now() at time zone 'UTC') where id IN ('Microsoft Course')

My code is below, your help is appreciated.

 

================================================================

class intrac_courses(osv.osv):

    def create(self, cr, uid, vals, context=None):
        if not vals:
            vals = {}
        if context is None:
           context = {}
        product_name = vals.get('name', '1')
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        product_obj.create(cr, uid, {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).create(cr, uid, vals, context=context)

    def write(self, cr, uid, ids, vals, context=None):
        product_name = vals.get('name', '2')
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        product_obj.write(cr, uid, [product_name], {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).write(cr, uid, vals, context=context)

    _name = 'intrac.courses'
    _columns = {
        'course_category': fields.selection([('General English','General English'),('Intensive English','Intensive English'),('English for Special Purposes','English for Special Purposes'),('Business Edge','Business Edge'),('Information Technology','Information Technology'),('Other','Other')], 'Course Category', required=True),
        'name': fields.char('Course Name', size=100, required=True),
        'course_arabic_name': fields.char('Arabic Name', size=100),
        'course_long_name': fields.char('Long Name', size=200, required=True),
        'course_duration': fields.integer('Duration (Hours)', required=True),
        'course_fee': fields.char('Fees Per Trainee'),
        'course_outline': fields.html('Course Outline & Details'),
        'course_notes': fields.one2many('intrac.courses.notes', 'course_name', 'Course Notes'),
    }
intrac_courses()

1

Emipro Technologies Pvt. Ltd.

--Emipro Technologies Pvt. Ltd.--
Tiny ERP Pvt Ltd
http://www.odoo.com
5975
| 6 7 8
Rajkot, India
--Emipro Technologies Pvt. Ltd.--

Being an emerging leader in IT market since 2011, Emipro Technologies Pvt. Ltd. has been providing a wide range of business solutions in Odoo & Magento. We are pleased to have a large pool of contented customers with our meticulous work in the domain of ERP & e-Commerce. A ray of relief & satisfaction to  our customers heart by our successful deployment in their organization, purely represents our success in Odoo platform. Hence, we take pride for being an Odoo partner with a vision of expanding our strategic alliances with our customers to offer them high value-added, trustworthy & cost effective solutions.

 

Since establishment, our business has span across 11 countries of the world. Our customers are companies of all sizes ranging from start-ups to large enterprises, who realize that they need a professional business solutions to generate revenue streams, establish proper communication channels or streamline business operations. Standing with strong determination of customer satisfaction, observing each minute detail of their business processes, providing proper guidance and moving forth to develop product accordingly adds our value and reliability among our customers. 

 

We are blessed with efficient, passionate & eclectic young developers who have come across almost all kinds of business profiles, working with full dedication, applying creativity & new features in existing modules and completing customer's projects successfully on predefined target. Our in-depth knowledge while giving business solutions in Odoo allows us to offer following services to our customers :

 

  • Consulting
  • Installation, Configuration & Customization
  • Training & Support
  • Maintenance
  • Upgradation
  • Documentation
  • Crafting community modules
  • Drafting videos on demand
  • Smart Apps development

We will be :) to welcome you @ www.emiprotechnologies.com to solve any kind of your business needs around Odoo. However we are just an inch away from you by email info@emiprotechnologies.com or a tweet to @EmiproTech

Emipro Technologies Pvt. Ltd.
On 12/6/14, 5:04 AM

Hello Abdullah

From your create method check these lines

product_name = vals.get('name', '1') ---> this is a "String"

product_obj.create(cr, uid, {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context) --> here when you create the product an "Integer" ID is returned for the new product / template.

-------------------------------------------------

Now check these lines from write method

product_name = vals.get('name', '2') ---> this will return a "String", say as per your error, it returned "Microsoft Course".

product_obj.write(cr, uid, [product_name], {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context) ---> here for 'write' method for any model you need is an "Integer" ID, instead you are passing the name which is a "String", which is wrong so it throws error.

You should have some realtion between your "intrac_course" model and product_template model, because once the course record is created you don't have any reference of "product_template" model, so during 'write' method how will you write into the 'product_template' model.

And for the access error, you need to check your access rules on that particular model.

1
Abdullah Sofan
On 12/6/14, 7:07 AM

Dear Empiro,

What is the right way to write to another object?

@ Empiro

I did this but now I am getting Assertion Error:

===============================
 

class intrac_courses(osv.osv):

    def create(self, cr, uid, vals, context=None):
        if not vals:
            vals = {}
        if context is None:
           context = {}
        product_name = vals.get('name', '1')
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        product_obj.create(cr, uid, {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).create(cr, uid, vals, context=context)

    def write(self, cr, uid, ids, vals, context=None):
        prod_id = vals.get('product_id', '3')
        product_name = vals.get('name', '2')
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        product_obj.write(cr, uid, prod_id, {'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).write(cr, uid, ids, vals, context=context)

    _name = 'intrac.courses'
    _columns = {
        'product_id': fields.many2one('product.template', 'Product ID'),
        'course_id': fields.char('Course ID'),
        'course_category': fields.selection([('General English','General English'),('Intensive English','Intensive English'),('English for Special Purposes','English for Special Purposes'),('Business Edge','Business Edge'),('Information Technology','Information Technology'),('Other','Other')], 'Course Category', required=True),
        'name': fields.char('Course Name', size=100, required=True),
        'course_arabic_name': fields.char('Arabic Name', size=100),
        'course_long_name': fields.char('Long Name', size=200, required=True),
        'course_duration': fields.integer('Duration (Hours)', required=True),
        'course_fee': fields.char('Fees Per Trainee'),
        'course_outline': fields.html('Course Outline & Details'),
        'course_notes': fields.one2many('intrac.courses.notes', 'course_name', 'Course Notes'),
    }
intrac_courses()

@abdullah as I said you should and "Integer" value which will be an Id, which will tell to the system that which record should be updated, so following is the way. You should do one thing, take "product_id" as many2one field of "product_product" in "intrac_courses" model, which will store value of that newly created product while you create a new course. Then while you update/write you can the find the reference of that product and write values in it. Following is the syntax product_obj.write(cr,uid,product_id,{your_values},context=context) Let me know if you still find it difficult

Emipro Technologies Pvt. Ltd.
on 12/6/14, 8:28 AM

@Empiro I am sorry but I am really a newbi here. What is the meaning of the "ids" inside the write method. What does the method expect when you put "ids" or "product_id" and what does it do with this value?

Abdullah Sofan
on 12/6/14, 10:16 AM

@ Empiro ... I did this but now I am having Assertion Error ============================== class intrac_courses(osv.osv): def create(self, cr, uid, vals, context=None): if not vals: vals = {} if context is None: context = {} product_name = vals.get('name', '1') product_fee = vals.get('course_fee', '0.0') product_desc = vals.get('course_outline', '') product_obj = self.pool.get('product.template') product_type = 'service' product_obj.create(cr, uid, {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context) return super(intrac_courses, self).create(cr, uid, vals, context=context) def write(self, cr, uid, ids, vals, context=None): prod_id = vals.get('product_id', '3') product_name = vals.get('name', '2') product_fee = vals.get('course_fee', '0.0') product_desc = vals.get('course_outline', '') product_obj = self.pool.get('product.template') product_type = 'service' product_obj.write(cr, uid, prod_id, {'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context) return super(intrac_courses, self).write(cr, uid, ids, vals, context=context) _name = 'intrac.courses' _columns = { 'product_id': fields.many2one('product.template', 'Product ID'), 'course_id': fields.char('Course ID'), 'course_category': fields.selection([('General English','General English'),('Intensive English','Intensive English'),('English for Special Purposes','English for Special Purposes'),('Business Edge','Business Edge'),('Information Technology','Information Technology'),('Other','Other')], 'Course Category', required=True), 'name': fields.char('Course Name', size=100, required=True), 'course_arabic_name': fields.char('Arabic Name', size=100), 'course_long_name': fields.char('Long Name', size=200, required=True), 'course_duration': fields.integer('Duration (Hours)', required=True), 'course_fee': fields.char('Fees Per Trainee'), 'course_outline': fields.html('Course Outline & Details'), 'course_notes': fields.one2many('intrac.courses.notes', 'course_name', 'Course Notes'), } intrac_courses()

Abdullah Sofan
on 12/6/14, 11:03 AM

@Abdullah In the write method of intrac_courses, you need to explicitly find the product id, because in he write method only those values will be filled in the "vals" which are modified on the screen, other fields are not there in it. So you need to explicitly find the product_id. Actually you need someone around you who can guide you properly. Discussing on the forum is not feasible to solve this kind of problem. Hope you understand. Best of luck... :)

Emipro Technologies Pvt. Ltd.
on 12/6/14, 12:25 PM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/6/14, 4:16 AM
Seen: 1226 times
Last updated: 3/16/15, 8:10 AM