Kiran
Kiran

Reputation: 1531

form view of odoo not working properly

I created a module called student. I have two form views. 1st form just consists of a button to redirect to 2nd form. 2nd form consists of few fields like first name, last name,dob,address,phone and email. When i am in 1st form and want to redirect to 2nd form it gives me an error called "Integrity error" means it is checking for not null constraint since i have made first_name and last_name fields as mandatory in 2nd form. Why it is checking 2nd form fields when i am in 1st form. But if i make those two fields as not mandatory everything works fine. so there is no problem in my redirection code. I dont want to remove required attribute on those two fields. I have attached the images and my code alsoenter image description here enter image description here

Here is my python code,

class student(osv.osv):
_name = 'student'
_columns = {
    'first_name': fields.char('First Name',size=30,required=True,    help='first name'),
    'name': fields.char('Last Name',size=30,required=True, help='last name'),
    'birth_date': fields.date('Birth Date',size=30, help='DOB'),
    'address': fields.text('Address',size=100, help='your Address'),
    'email': fields.char('Email',size=50, help='the email'),
    'phone': fields.char('Phone',size=30, help='the phone'),
}

@api.multi
def redirection(self):
    view_id = self.env.ref('student.view_student_form').id
    context = self._context.copy()

    return {
        'name':'student.form',
        'view_type':'form',
        'view_mode':'form',
        'views' : [(view_id,'form')],
        'res_model':'student',
        'view_id':view_id,
        'type':'ir.actions.act_window',
        'res_id':self.id,
        'target':'current',
        'context':context,
    }

@api.multi
def redirection2(self):
    view_id = self.env.ref('student.view_student2_form').id
    context = self._context.copy()

    return {
        'name':'student2.form',
        'view_type':'form',
        'view_mode':'form',
        'views' : [(view_id,'form')],
        'res_model':'student',
        'view_id':view_id,
        'type':'ir.actions.act_window',
        'res_id':self.id,
        'target':'current',
        'context':context,
    }

Xml code opening tag of data,openerp not appearing here so dont worry about that,its there in my code student.form student form


    <record model="ir.ui.view" id="view_student2_form">
        <field name="name">student2.form</field>
        <field name="model">student</field>
        <field name="priority" eval="15"/>
        <field name="type">form</field>
        <field name="arch" type="xml">
        <form string="student">
        <group>
            <field name="first_name" style="width: 40%"/>  
            <field name="name" style="width: 40%"/> 
            <button type="object" string="Form1" name="redirection"/>
        </group>    
        </form>
        </field>
    </record>

    <record model="ir.ui.view" id="view_student_tree">
        <field name="name">student.tree</field>
        <field name="model">student</field>
        <field name="type">tree</field>
        <field name="arch" type="xml">
            <tree string="student">
                <field name="first_name"/>
                <field name="name"/>
                <field name="birth_date"/>
                <field name="address"/>
                <field name="email"/>
                <field name="phone"/>
            </tree>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_student">
        <field name="name">Student</field>
        <field name="res_model">student</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
    </record>

    <record model="ir.actions.act_window" id="action_student2">
        <field name="name">Student.action2</field>
        <field name="res_model">student</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="view_id" ref="view_student_form"/>
    </record>

    <menuitem name="Student/Student" id="menu_student" action="action_student"/>
    <menuitem name="Student" id="menu_student_student_item" parent="menu_student" action="action_student"/>
    </data>
    </openerp>

Upvotes: 1

Views: 2453

Answers (2)

Tintumon M
Tintumon M

Reputation: 1164

If you once coded required=True in .py file then it'll set the Not NULL = Yes in database table.

I won't change if you check the table property even after you remove the required=True from .py file.

Solution : After removing the required=True just upgrade that particular module from installed modules in setting. Then only they changes you made will reflect in db and it works.

Upvotes: 0

William Wino
William Wino

Reputation: 3819

Because you defined those fields as mandatory inside the model declaration.

You need to remove required=True from the model declaration (from .py file).

'first_name': fields.char('First Name',size=30,help='first name'),

And put the constraint in the view declaration instead (the .xml file):

<field name="first_name" required="True"/>

Upvotes: 1

Related Questions