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.

0

Error while creating Wizard

By
evon_dun
on 6/4/13, 7:45 AM 2,048 views

Having problem while creating wizard on my own simple module that simply add students and subject with wizard. i tried creating it and the module is upgraded successfully but when i try to invoke the wizard i get this error:

ValueError: Invalid field 'user_id' in domain expression [('user_id', '=', 1)]

Please help and the function where it raises the error is as below:

 def _get_subject_id(self, cr, uid, context=None):
        student_id = context.get('subject_id', self.pool.get('subject').search(cr, uid, [('user_id', '=', uid)], context=context))
        if subject_id:
            employee = self.pool.get('subject').browse(cr, uid, subject_id, context=context)[0]
            return {'name': subject.name,'subject_id': subject_id[0]}
        return {}

class add_subject(osv.osv_memory):
    _name = 'add.subject'
    _description = 'Add Subject Directly'
    _columns = {
        'name': fields.char('Subject', size=132),        
        'subject_id': fields.many2one('subject', 'Subject Id'),
        }

    def _get_subject(self, cr, uid, context=None):
        subject_id = context.get('subject_id', self.pool.get('subject').search(cr, uid, [('user_id', '=', uid)], context=context))
        if subject_id:
            subject = self.pool.get('subject').browse(cr, uid, subject_id, context=context)[0].fname
            return subject
        return ''
    def _get_subject_id(self, cr, uid, context=None):
        subject_id = context.get('subject_id', self.pool.get('subject').search(cr, uid, [('user_id', '=', uid)], context=context))
        if subject_id:
            return subject_id[0]
        return False

    _defaults = {
         'name': _get_subject,
         'subject_id': _get_subject_id,
                 }

    def add_subject(self, cr, uid, ids, context=None):
        data = self.read(cr, uid, ids, [], context=context)[0]
        data['subject_id'] = data['subject_id'] and data['subject_id'][0]
        return self.pool.get('add.new.subject').add_subject(cr, uid, data, context)

    def remove_subject(self, cr, uid, ids, context=None):
        data = self.read(cr, uid, ids, [], context=context)[0]
        data['subject_id'] = data['subject_id'] and data['subject_id'][0]
        return self.pool.get('add.new.subject').remove_subject(cr, uid, data, context)

add_subject()

class add_new_subject(osv.osv_memory):

    _name='add.new.subject'
    _columns = {
            'name': fields.char('Subject name', size=32,),           
            'subject_id': fields.many2one('subject', 'Subject ID',)
            }


    def _get_subject_id(self, cr, uid, context=None):
        student_id = context.get('subject_id', self.pool.get('subject').search(cr, uid, [('user_id', '=', uid)], context=context))
        if subject_id:
            employee = self.pool.get('subject').browse(cr, uid, subject_id, context=context)[0]
            return {'name': subject.name,'subject_id': subject_id[0]}
        return {}

    def default_get(self, cr, uid, fields_list, context=None):
        res = super(add_new_subject, self).default_get(cr, uid, fields_list, context=context)
        res_subject = self._get_subject_id(cr, uid, context=context)
        res.update(res_subject)
        return res

    def sub_check(self, cr, uid, ids, context=None):
        obj_model = self.pool.get('ir.model.data')
        att_obj = self.pool.get('subject')
        data = self.read(cr, uid, ids, [], context=context)[0]
        data['subject_id'] = data['subject_id'] and data['subject_id'][0]
        subject_id = data['subject_id']
        att_id = att_obj.search(cr, uid, [('subject_id', '=', emp_id),('action', '!=', 'action')], limit=1, order='name desc')
        last_att = att_obj.browse(cr, uid, att_id, context=context)
        if last_att:
            last_att = last_att[0]
        if not att_id and not last_att:
            model_data_ids = obj_model.search(cr, uid, [('model','=','ir.ui.view'),('name','=','view_add_subject_message')], context=context)
            resource_id = obj_model.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
            return {
                'name': _('Add Subject/ Remove Subject'),
                'view_type': 'form',
                'view_mode': 'tree,form',
                'res_model': 'add.new.subject',
                'views': [(resource_id,'form')],
                'type': 'ir.actions.act_window',
                'context': context,
                'target': 'new',
            }

    def add_subject(self, cr, uid, data, context=None):
        if context is None:
            context = {}
        subject_id = data['subject_id']
        if 'name' in data:
            raise osv.except_osv(_('UserError'), _('Subject already added'))
            self.pool.get('subject').create(cr, uid, {'name': data['name'], 'action': 'remove_subject',
                'subject_id': subject_id}, context=context)
        try:
            self.pool.get('subject').subject_action_change(cr, uid, [subject_id], 'add_subject')
        except:
            raise osv.except_osv(_('UserError'), _('Same subject can be added only after deletion'))
        return {'type': 'ir.actions.act_window_close'} # To do: Return Success message

    def remove_subject(self, cr, uid, data, context=None):
        subject_id = data['subject_id']
        if 'name' in data:
            raise osv.except_osv(_('UserError'), _('Subject name should be different'))
            self.pool.get('subject').create(cr, uid, {'name':data['name'], 'action':'add_subject',  'subject_id':subject_id}, context=context)
        try:
            self.pool.get('subject').subject_action_change(cr, uid, [subject_id], 'remove_subject')
        except:
            raise osv.except_osv(_('UserError'), _('Same subject can be added only after deletion'))
        return {'type': 'ir.actions.act_window_close'} # To do: Return Success message

add_new_subject()

<openerp> <data> <record id="view_add_new_subject" model="ir.ui.view"> <field name="name">add.new.subject.form</field> <field name="model">add.new.subject</field> <field name="type">form</field> <field name="arch" type="xml"> <form string="Subject Add / Subject Remove"> <separator colspan="4" string="Subject Add / Subject Remove"/> <label colspan="4" nolabel="1" string="If you need to add subject OpenERP allows you to manage this with this tool."/> <newline/> <group colspan="4" >="" <field="" name="name"/>
</group> <separator colspan="4"/> <group colspan="4" col="6"> <button icon="gtk-cancel" special="cancel" string="Cancel"/> <button icon="terp-gtk-jump-to-ltr" string="Add Subject" name="sub_check" type="object"/> <button icon="terp-gtk-jump-to-rtl" string="Remove Subject" name="sub_check" type="object"/> </group> </form> </field> </record>

    <record id="view_add_subject_message" model="ir.ui.view">
        <field name="name">remove.subject.form</field>
        <field name="model">add.new.subject</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Subject Add / Subject Remove">
                <separator string="You must different subject." colspan="4" />
                <group colspan="4" col="6">
                    <button icon="gtk-cancel" special="cancel" string="Ok"/>
               </group>
           </form>
        </field>
    </record>

    <record id="action_add_new_subject" model="ir.actions.act_window">
        <field name="name">Subject Add / Subject Remove</field>
        <field name="res_model">add.new.subject</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
       <field name="view_id" ref="view_add_new_subject"/>
       <field name="target">new</field>
       <field name="help">Subject Add / Subject Remove</field>
    </record>

    <menuitem action="action_add_new_subject" id="menu_add_new_subject"
        parent="menu_subject" sequence="2"/>

    <record id="view_add_subject" model="ir.ui.view">
        <field name="name">add.subject.form</field>
        <field name="model">add.subject</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="add.subject">
                <group colspan="4" >
                <separator string="You cannot add subject with same name. Please enter some other subject." colspan="4" />
                <field name="name" />                    
                </group>
                <separator colspan="4" />
                <group colspan="4" col="6">
                    <button icon="gtk-cancel" special="cancel" string="Cancel"/>
                    <button icon="gtk-go-back" string="Add subject" name="add_subject" type="object"/>
               </group>
           </form>
        </field>
    </record>

    <record id="view_remove_subject" model="ir.ui.view">
        <field name="name">add.remove.subject.form</field>
        <field name="model">add.subject</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="remove.subject">
                <group colspan="4" >
                <separator string="Are you sure you want to remove this subject." colspan="4" />
                <field name="name" />                    
                </group>
                <separator colspan="4" />
                <group colspan="4" col="6">
                    <button icon="gtk-cancel" special="cancel" string="Cancel"/>
                    <button icon="gtk-go-back" string="Remove Subject" name="remove_subject" type="object"/>
               </group>
           </form>
        </field>
    </record>
</data>

</openerp>

subject class:

class subject(osv.osv): _name = 'subject' _description = 'Subjects' _columns = { 'name':fields.char('Subject',size=64), 'subject_id':fields.char('Code',size=24), } _order = 'name desc'

def _action_check(self, cr, uid, emp_id, dt=False, context=None):
    cr.execute('SELECT name FROM subject WHERE subject_id=%s', (subject_id,))
    res = cr.fetchone()
    return res

def subject_action_change(self, cr, uid, ids, type='action', context=None, dt=False, *args):
    obj_subject = self.pool.get('subject')
    id = False
    warning_sign = 'subject'
    res = {}

    #Special case when button calls this method: type=context
    if isinstance(type, dict):
        type = type.get('type','action')
    if type == 'add_subject':
        warning_sign = "Subject Add"
    elif type == 'remove_subject':
        warning_sign = "Subject Remove"
    for subject in self.read(cr, uid, ids, ['id'], context=context):
        if not self._action_check(cr, uid, subject['id'], dt, context):
            raise osv.except_osv(_('Warning'), _('Wrong action! Contact Administrator')%(warning_sign,))

        res = {'action': type, 'subject_id': subject['id']}

    id = obj_subject.create(cr, uid, res, context=context)

    if type != 'action':
        return id
    return True subject()
0

Borni DHIFI

--Borni DHIFI--
5102
| 6 8 9
Tunis, Tunisia
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

http://www.linkedin.com/in/borni

Skype : dhifi.borni

Borni DHIFI
On 6/4/13, 8:00 AM

Hi,

your class subject does not contain a field user_id .

verify your class subject.

yes i don't have any column like user_id but i tried to create this just like hr_attendance module, they have used user_id but there its not in the list of columns at all. 20227 (just now)edit

evon_dun
on 6/4/13, 8:25 AM
0

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 6/4/13, 8:00 AM

According to error it seems subject object does not have field user_id.

Can you please post _columns of subject object?

yes i don't have any column like user_id but i tried to create this just like hr_attendance module, they have used user_id but there its not in the list of columns at all.

evon_dun
on 6/4/13, 8:25 AM

You cannot use any field which is not in your object.

Sudhir Arya (SA)
on 6/4/13, 8:39 AM

hey i added user_id as subject_id and tried again but still it gives the same error. Please look at the hr_attendance module and guide me.

evon_dun
on 6/4/13, 9:40 AM

Can you post your wizard py and xml file? That will be better to solve your issue.

Sudhir Arya (SA)
on 6/4/13, 9:55 AM

Did you restart your server after addins 'user_id' field in subject object?

Sudhir Arya (SA)
on 6/4/13, 10:46 AM

yes of course

evon_dun
on 6/5/13, 2:49 AM

Can you please post code of your subject class?

Sudhir Arya (SA)
on 6/5/13, 3:16 AM

So its clear now that your subject class does not have user_id field. You need to create that field in subject class and then restart you server.

Sudhir Arya (SA)
on 6/5/13, 3:37 AM

Have you checked hr_attendance module???

evon_dun
on 6/5/13, 3:38 AM

Ary you talking about def _employee_get method in which a domain is used in search method of hr.emoloyee?

Sudhir Arya (SA)
on 6/5/13, 3:44 AM

yes may be i got confused.

evon_dun
on 6/5/13, 3:54 AM

http://help.openerp.com/question/13569/sum-function-in-reports/ pls check this question too may be you know the solution

evon_dun
on 6/5/13, 3:54 AM

The field user_id is already created in hr module in 'hr.employee' class. So you also have to create that field.

Sudhir Arya (SA)
on 6/5/13, 4:10 AM

thanks a lot

evon_dun
on 6/5/13, 4:35 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

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

Question tools

0 follower(s)

Stats

Asked: 6/4/13, 7:45 AM
Seen: 2048 times
Last updated: 3/16/15, 8:10 AM