This question has been flagged
2 Replies
5795 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()
Avatar
Discard
Best Answer

Hi,

your class subject does not contain a field user_id .

verify your class subject.

Avatar
Discard
Author

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

Best Answer

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

Can you please post _columns of subject object?

Avatar
Discard
Author

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.

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

Author

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.

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

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

Author

yes of course

Can you please post code of your subject class?

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.

Author

Have you checked hr_attendance module???

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

Author

yes may be i got confused.

Author

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

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

Author

thanks a lot