Skip to Content
Menu
This question has been flagged
1 Reply
1830 Views

When approved,how to add all  fields datas  from  one model to another models fields data ..

After I approved my application form ,then after that how to transfer alll field data from one model i.e application to my students details menu  model(student_student) fields...How to implement it...

My  Model is:-

# -*- coding: utf-8 -*-

from odoo import models, fields, api, tools, _
from datetime import datetime, date
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
from odoo.exceptions import ValidationError

#Model of student
class StudentStudent(models.Model):
    _name = 'student.student'
    _order = "admission_no desc"

    name = fields.Char(string = 'Name', required = True)
    student_dob = fields.Date(string = "Date of Birth")
    age = fields.Integer(string = 'Age', compute = 'calcu_age',
        store = True)
    parent_name = fields.Char(string = 'Parent Name', required = True)
    parent_email = fields.Char(string ='Parent Email ID',required = True)
    admission_no = fields.Integer('Admission No', required = True)
    place = fields.Char(string = 'Place', required = True)
    photo = fields.Binary(string = 'Image')
    phone_number = fields.Char(string = 'Phone Number', required = True)
    gender = fields.Selection([('male', 'Male'),
                            ('female', 'Female'),
                            ('others', 'Others')],
                            string = 'Gender')
    student_blood_group = fields.Selection([('A+', 'A+ve'),
                                            ('B+', 'B+ve'),
                                            ('O+', 'O+ve'),
                                            ('AB+', 'AB+ve'),
                                             ('A-', 'A-ve'),
                                             ('B-', 'B-ve'),
                                             ('O-', 'O-ve'),
                                             ('AB-', 'AB-ve')],
                                             string = 'Blood Group')
    nationality = fields.Many2one('res.country', string = 'Nationality')
    branchname = fields.Many2one('student.branch', string = 'Branchname')
    active = fields.Boolean(default = True)
    #filteration = fields.Selection(selection = 'addfilter')
    #sortingfunction = fields.Selection(selection = 'addsort')
    #creating = fields.Char(compute = 'action_create')
    #browsing = fields.Char(compute = 'action_browse')
    #Existing=fields.Char(compute='action_exists')
   
    #Method to display name and place using name_get method
    @api.multi
    def name_get(self):
        '''Method to display name and place'''
        return [(rec.id, '[' + rec.name + ']' + rec.place) for rec in self]

    # Validation of Phone Number
    @api.constrains('phone_number')
    def _check_phone_number(self):
        if len(self.phone_number) != 10:
            raise ValidationError(_("Invalid Phone Number..."))

    # Create Orm method for creating a record
    @api.multi
    def action_create(self):
        for record in self:
            if record.name == 'lilly':
                record.create({'name': "Joe",
                                'admission_no': 456,
                                'place': "rtr"})

    #Method to calculate age fom Date of Birth
    @api.depends('student_dob')
    def calcu_age(self):
        '''Method to calculate student age'''
        current_dt = datetime.today()
        for rec in self:
            if rec.student_dob:
                start = datetime.strptime(str(rec.student_dob), DEFAULT_SERVER_DATE_FORMAT)
                age_calc = ((current_dt - start).days / 365)
                # Age should be greater than 0
                if age_calc > 0.0:
                    rec.age = age_calc

#Model of Admission
class Admission(models.Model):
    _name = 'student.admission'
    _rec_name='fname'
   
    fname = fields.Char(string = 'First Name', required = True)
    lname = fields.Char(string = 'Last Name', required = True)
    student_dob = fields.Date(string = "Date of Birth")
    age = fields.Integer(string = 'Age')
    admission_class=fields.Selection([('lkg','LKG'),
                                      ('ukg','UKG'),
                                      ('firststd','First Standard')],
                                      string='Admission Class',required = True)              
    photo = fields.Binary(string = 'Image')
    gender = fields.Selection([('male', 'Male'),
                            ('female', 'Female'),
                            ('others', 'Others')],
                            string = 'Gender')
    place = fields.Char(string = 'Place', required = True)
    #hide_inv_button = fields.Boolean(copy = False, default = True)
    pincode = fields.Integer(string = 'PinCode',required=True)                           
    nationality = fields.Many2one('res.country', string = 'Nationality')
    Residence_phoneno =fields.Char(string='Residence Phone Number',required =True)
    Mothers_name =fields.Char(string='Mothers Name',required=True)
    Mothers_qualification=fields.Char(string='Mothers Qualificatioin')
    Fathers_name=fields.Char(string='Fathers Name')
    Fathers_qualification=fields.Char(string='Fathers Qualification')
    have_sibling=fields.Boolean(string='Have any Sibling in this school')
    sibling_name=fields.Char(string='Sibling name',default=False)
    state = fields.Selection([
            ('draft', 'Draft'),
            ('in_progress', 'In Progress'),
            ('done', 'Done'),
            ('approved', 'Approve')],
            default = 'draft')           
   
    @api.multi
    def action_confirmed(self):
        self.ensure_one()
        self.write({'state': 'in_progress'})
   
    @api.multi
    def action_finished(self):
        self.ensure_one()
        self.write({'state': 'done'})
       
    #Method of Approve Button
    @api.multi
    def action_approve(self):
        self.ensure_one()
        self.write({'state': 'approved'})                    
  Please Help...

Thanks in Advance 
                               

Avatar
Discard
Best Answer

Hi,

With data from the current model you can create a record in another model by calling the create method the second model,

Suppose you have a model ABC with field1, field2 and field3, suppose upon a button click if you want to generate a record in model DEF,

just call the create method of and pass the values,

@api.multi
def button_click(self):
for rec in self:
vals = {
'field_1': rec.field1,
'field_2': rec.field2,
'field_3': rec.field2
}
self.env['model_def'].create(vals)

Thanks

Avatar
Discard