0
votes

I am a newbie to python and odoo. I am developing a custom module to implement student application for a school. My primary aim is to collect application data. I have 3 models for this.

class ApApplicationsModel(models.Model):
    _name = 'ap.applications.model'    

class ApPrevSchoolModel(models.Model):    
    _name = 'ap.prevschool.model' 

class ApPersonModel(models.Model):    
    _name = 'ap.person.model' 
    _inherit = 'op.student'

ApPersonModel will be used to store the details of student, parent and guardian. To this effect i have an invisible selection attribute ap_persontype

3 many2one links have been defined in the

ap_father_id = fields.Many2one('ap.person.model','Father Details', readonly=False,related='ap_prevschool_model.ap_name', store=True)

ap_mother_id = fields.Many2one('ap.person.model','Mother Details', readonly=False)

ap_student_id = fields.Many2one('ap.person.model','Student Details')

I have a field

ap_persontype = fields.Selection(
        [('student','Student'),('parent','Parent'),('grd','Guardian')],  'Person Type',default='student', track_visibility='onchange') 

in ap.person.model

I want to set the type of person to 'student' or 'parent' or 'guardian' as the form view gets initiated to show and hide some fields.

How do i do this ?

It mayn't be a good oo practice to deal like this. How do i then achieve it ?

2
my first concern is how do i set ap_persontype to a 'parent' or 'grd' when the form is loaded / initialized depending on the many2one object being constructed by the view. my second concern is about enabling or disabling fields. I am struggling to identify the correct way to address my first concern.Mohammed

2 Answers

0
votes

I dont know what you exactly want to achieve but i guess that you want to hide or show some fields on by condition of something (type of person in this case). So what you want to do is to make attribute values to all the field what you want to hide or show and then it will automatically hide or show. mini example:

<field name="student_age" attrs="{'invisible': [('ap_persontype'),'not in',['parent','grd']]}"/>

I hope it helped you.

0
votes
  • Please use only one module for this logic no need to use multiple models and try this code:
  • If ap_persontype = Student then invisible other fields. Same logic for another field.
    Here my code logic is

    <field name="ap_student_id" attrs="{'invisible': [('ap_persontype'),'not in',['student',]]}"/> <field name="ap_mother_id" attrs="{'invisible': [('ap_persontype'),'not in',['parent',]]}"/> <field name="ap_father_id" attrs="{'invisible': [('ap_persontype'),'not in',['parent',]]}"/>