Set default values for One2many field

无人久伴 提交于 2021-02-08 10:18:23

问题


I have a class student_student which have a one2many fieldresult_ids defined like the following:

    result_ids = fields.One2many("schoolresults.detail", "student_id", "School Results", default="_get_subjects")

and

def _get_subjects(self):
     cr = self.pool.cursor()
     self.env
     return self.pool.get('schoolresults.subject').search(cr, self.env.uid, [])

in the other side I have a class schoolresults_subject:

class schoolresults_subject(models.Model):
    _name = "schoolresults.subject"
    _description = "Student's subjects."
    name = fields.Char("Subject")

class schoolresults_detail(models.Model):
    _name = "schoolresults.detail"
    _description = "Student's results."
    student_id = fields.Many2one("student.student", "Student", ondelete="cascade")
    subject_id = fields.Many2one("schoolresults.subject", "Subject")
    result = fields.Float("Result", compute='_compute_value', store=True)

What I'm trying to do is to fill the result_ids with a subjects list from the last class, whenever the user trying to create a new student profile, using the the default parameter in the one2many field. But whenever I try to create a student profile I get this error Wrong values for student.student.result_ids. Please is there anyway to achieve that?

PS. I'm using Odoo 9


回答1:


I don't get your requirements here fully, but try something like the following:

def _get_subjects(self):
    subjects = self.env['schoolresults.subject'].search([])
    details = self.env['schoolresults.detail']
    for subject in subjects:
        details |= details.new({'subject_id': subject.id})
    return details

But explaining the error message: you're returning a RecordSet of schoolresults.subject but your field result_ids has the comodel schoolresults.detail. That's just wrong ;-)




回答2:


I could do this by overriding the default_get method:

def default_get(self, fields):
    res = super(student_student, self).default_get(fields)
    srd = self.env['schoolresults.detail']
    ids=[]
    school_result={'subject_id':1,'result':0} #dict for fields and their values
    sr = srd.create(school_result)
    ids.append(sr.id)


    res['result_ids'] = ids
    return res

This is how to override default_get for one2many field.

Credit goes to:Default values for one2many




回答3:


Please read this If you want to set the default values of one2many field from action, you can create a list of tuple with the same format we use for create or write one2many field and then you can pass it to context. You can copy your context-

context = self.env.context.copy()

Prepare one2many values like this--

pr_lines = []
        for pr_line in pr_obj.line_ids: #loop for multiple lines.
            pr_lines.append((0,0, {
                'purchase_request_id' : pr_obj.id,
                'product_id' : pr_line.product_id.id, 
                'description' : pr_line.name,
                'qty_transfer' : pr_line.product_qty,
                'uom_id' : pr_line.product_uom_id.id,
            }))

You can update your context like this-

    context.update({
            'default_warehouse_id': self.approving_matrix_id.warehouse.id,
            'default_internal_transfer_receipt': pr_lines,
        })

in the action you can pass context like 'context' : context,




回答4:


result_ids = fields.Many2many("schoolresults.detail", "student_id", "School Results", default="_get_subjects")

def get_default_lines(self):
    obj = self.env['your.class.name'].search([])
    return obj

Try adding many2many field to your class.



来源:https://stackoverflow.com/questions/38928685/set-default-values-for-one2many-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!