콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
10 답글
15993 화면

Hello 

i have the following models . i would like to create a new record in model b once the state of record a switches to unit manager approved . i was able to achieve that with automated actions but dont know how to bring the parts from model a_lines to model b_lines . i would appreciate any pointers 

model a 


class ServiceRequest(models.Model):
_name = 'servicerequest.rider'
_rec_name = 'jobcard_no'
_inherit = ['mail.thread', 'mail.activity.mixin']

    vehicle_id = fields.Many2one('vehicles.rider')
    state = fields.Selection(string="", selection=[('check-in', 'check-in'), ('Technician service completed', 'Technician service completed'), ('Unit Manager parts approved', 'Unit Manager parts approved'), ('store officer parts released', 'store officer parts released'), ('Unit manager quality check', 'Unit manager quality checked'), ('Checked out', 'Checked out'), ], default='check-in', required=False, track_visibility=True, trace_visibility='onchange', )
    jobcard_no = fields.Char(string="Jobcard Number",
default=lambda self: self.env['ir.sequence'].next_by_code('increment_jobcard'),
requires=False, readonly=True, )
    operations = fields.One2many('jobcard.partsline', 'servicerequest_id', 'Parts',
                     copy=True, readonly=True, states={'check-in': [('readonly', False)]})

@api.multi
def write(self, vals):
if vals.get('state'):
if vals.get('state') == 'Confirm':
lines_dict = {
'jobcard_id': self.id,
'operations': [(6, 0, self.operations.parts_id.ids)],
}
self.env['fundrequestw.rider'].create(lines_dict)
return super(ServiceRequest, self).write(vals)




Model a_lines


class JobcardParts(models.Model):
_name = 'jobcard.partsline'

_description = 'Parts Required used'

name = fields.Text(string='Description', required=False)
servicerequest_id = fields.Many2one(comodel_name="servicerequest.rider", index=True, ondelete='cascade')
parts_id = fields.Many2one('product.product', string='Parts',
ondelete='restrict', index=True)
quantity = fields.Integer(string="Quantity", required=False, )


model b 


class FundRequestWorkshop(models.Model):
_name = 'fundrequestw.rider'
_rec_name = 'request_no'
_inherit = ['mail.thread']


_description = 'Fund request workshop'

date = fields.Date(string="Date", default=date.today(), required=False, readonly=True, states={'draft': [('readonly', False)]})
request_no = fields.Char(string="Request Number", default=lambda self: self.env['ir.sequence'].next_by_code('increment_fund_request'), requires=False, readonly=True, trace_visibility='onchange',)
programme_id = fields.Many2one(comodel_name="programme.rider", string="Programme ID", required=False, readonly=True, states={'draft': [('readonly', False)]})
jobcard_id = fields.Many2one(comodel_name="servicerequest.rider", string="Job Card ref", required=False, readonly=True, states={'draft': [('readonly', False)]})
state = fields.Selection(string="", selection=[('draft', 'draft'), ('Requested', 'Requested'), ('Approved', 'Approved'), ('Rejected', 'Rejected'),], required=False, copy=False, default='draft', readonly=True, track_visibility='onchange', )
operations = fields.One2many(
'jobcard.partsline', 'fundrequest_id', 'Parts',
copy=True, readonly=True, states={'draft': [('readonly', False)]},)
part_qty = fields.Float(string="Quantity", required=False, )


Model b_lines


class FundrequestLine(models.Model):
_name = 'fundrequest.partsline'

_description = 'Parts request line'

name = fields.Text(string='Description', required=False)
fundrequest_id = fields.Many2one(comodel_name="fundrequestw.rider", index=True, ondelete='cascade')
parts_id = fields.Many2one('product.product', string='Parts',
ondelete='restrict', index=True)
아바타
취소
베스트 답변

In model a you'll need to overide the write method and there check the state and then create the record in model b.

@api.multi
def write(self, vals):
if vals.get('state'):
if vals.get('state') == 'Unit Manager parts approved'     # you should really use something shorter 
lines_dict # here get the lines that you want in a dict formatted for creating record in model b lines
self.env['fundrequest.partsline'].create(lines_dict) # here you pass the dict with vals that you want

return super(ServiceRequest, self).write(vals)
아바타
취소

sorry I don't know how to use this new WYSIWYG editor. So that's why the code looks so bad.

Why not use an onchange on the state field and just do a self.env['your.model'].create() though?

작성자

Thank You for your answer . i have followed your advice but i now have an error (ValueError: Expected singleton: jobcard.partsline(25, 26) )

That's because you're passing more than one record. You're passing the records with id 25 and 26. You need to pass them one by one. I don't know where this happens but try to do that line with "for rec in self:" (iterate the records one by one in the recordset) and call the method on rec and not on self. Living a like also helps :)

작성자

Please can you help me with a sample code based on earlier example

I can't because I don't know on what line it throws this error and what are you doing on that line. You're probably calling a method with the self parameter. so the only help I can offer you is like I wrote in the earlier comment you need to use a for loop to iterate trough the objects.

for rec in self:

rec.the_method_that_throws_error()

But I don't know if that's the problem or something else. Edit your question with the code that throws the error.

작성자

I have edited the question to include the code

I think this line is wrong "'operations': [(6, 0, self.operations.parts_id.ids)]," it needs to be 'operations': [(0, 0, {vals_dict1}), (0, 0, {vals_dict2}), ...]. I normally use (6, 0, [ids]) on many2many fields not one2many. But I might be wrong.

베스트 답변

Did you solve the issue?

아바타
취소
작성자

yes

관련 게시물 답글 화면 활동
0
10월 23
1207
1
2월 21
4709
3
6월 20
5318
0
6월 20
2890
0
2월 16
3935