ผลต่างระหว่างรุ่นของ "Tutorial Odoo 9.0 Part 8 : Onchange & Model constraints"
Sirawich (คุย | มีส่วนร่วม) ล |
Sirawich (คุย | มีส่วนร่วม) |
||
แถว 72: | แถว 72: | ||
<br/><br/> | <br/><br/> | ||
[[ไฟล์:Odoo8-3.png]] | [[ไฟล์:Odoo8-3.png]] | ||
− | + | <br/><br/> | |
*<h3>SQL Constrains</h3> | *<h3>SQL Constrains</h3> | ||
แก้ไขไฟล์ดังนี้ | แก้ไขไฟล์ดังนี้ | ||
แถว 109: | แถว 109: | ||
<br/><br/> | <br/><br/> | ||
[[ไฟล์:Odoo8-5.png]] | [[ไฟล์:Odoo8-5.png]] | ||
+ | <br/><br/><br/> | ||
+ | *<h3>Add a duplicate option</h3> | ||
+ | แก้ไขไฟล์ดังนี้ | ||
+ | <br/><br/> | ||
+ | openacademy/models.py | ||
+ | <pre> | ||
+ | session_ids = fields.One2many( | ||
+ | 'openacademy.session', 'course_id', string="Sessions") | ||
+ | |||
+ | @api.multi | ||
+ | def copy(self, default=None): | ||
+ | default = dict(default or {}) | ||
+ | |||
+ | copied_count = self.search_count( | ||
+ | [('name', '=like', u"Copy of {}%".format(self.name))]) | ||
+ | if not copied_count: | ||
+ | new_name = u"Copy of {}".format(self.name) | ||
+ | else: | ||
+ | new_name = u"Copy of {} ({})".format(self.name, copied_count) | ||
+ | |||
+ | default['name'] = new_name | ||
+ | return super(Course, self).copy(default) | ||
+ | |||
+ | _sql_constraints = [ | ||
+ | ('name_description_check', | ||
+ | 'CHECK(name != description)', | ||
+ | </pre> | ||
+ | <br/> | ||
+ | เพิ่ม option duplicate ให้กับ course ดังภาพ | ||
+ | <br/> | ||
+ | [[ไฟล์:Odoo8-6.png]] | ||
+ | <br/><br/><br/> | ||
+ | และเมื่อคลิกที่ duplicate ก็จะทำการสร้าง course ใหม่ ชื่อว่า <b>Copy of My First Course</b> ดังภาพ | ||
+ | <br/> | ||
+ | [[ไฟล์:Odoo8-7.png]] | ||
+ | <br/><br/> | ||
+ | หัวข้อถัดไป Tutorial Odoo 9.0 Part 9 : |
รุ่นแก้ไขเมื่อ 08:54, 15 กรกฎาคม 2559
เนื้อหา
Onchange
แก้ไขไฟล์ดังนี้
openacademy/models.py
r.taken_seats = 0.0 else: r.taken_seats = 100.0 * len(r.attendee_ids) / r.seats @api.onchange('seats', 'attendee_ids') def _verify_valid_seats(self): if self.seats < 0: return { 'warning': { 'title': "Incorrect 'seats' value", 'message': "The number of available seats may not be negative", }, } if self.seats < len(self.attendee_ids): return { 'warning': { 'title': "Too many attendees", 'message': "Increase seats or remove excess attendees", }, }
เมื่อมีการปรับเปลี่ยนค่าของ seats หรือ attendee จะทำให้ _verify_valid_seat ทำงาน
หากค่าของ seats น้อยกว่า 0 จะมีข้อความแจ้งเตือนดังภาพ
หากค่าของ seats น้อยกว่า attendee จะมีข้อความแจ้งเตือนดังภาพ
Model Constraints
Python Constrains
แก้ไขไฟล์ดังนี้
openacademy/models.py
ส่วนที่ 1
# -*- coding: utf-8 -*- from openerp import models, fields, api, exceptions class Course(models.Model): _name = 'openacademy.course'
ส่วนที่ 2
'message': "Increase seats or remove excess attendees", }, } @api.constrains('instructor_id', 'attendee_ids') def _check_instructor_not_in_attendees(self): for r in self: if r.instructor_id and r.instructor_id in r.attendee_ids: raise exceptions.ValidationError("A session's instructor can't be an attendee")
สร้างกฏเพื่อตรวจสอบว่า attendee กับ instructor ของ session นั้นๆ เป็นคนเดียวกันหรือไม่
หากเป็นคนเดียวกัน จะแสดงหน่าต่างแจ้งข้อผิดพลาด ดังภาพ
SQL Constrains
แก้ไขไฟล์ดังนี้
openacademy/models.py
session_ids = fields.One2many( 'openacademy.session', 'course_id', string="Sessions") _sql_constraints = [ ('name_description_check', 'CHECK(name != description)', "The title of the course should not be the description"), ('name_unique', 'UNIQUE(name)', "The course title must be unique"), ] class Session(models.Model): _name = 'openacademy.session'
สร้างกฏทำนองเดียวกับ Python Constrains
กรณีนี้เพื่อตรวจสอบว่า title และ description ของ Course นั้นต้องไม่เหมือนกัน
หากเหมือนกัน จะแสดงหน้าต่างแจ้งข้อผิดพลาด ดังภาพ
และ title ของแต่ละ course ต้องไม่ซ้ำกับ course อื่นๆ
หากซ้ำกันจะแสดงข้อผิดพลาด ดังภาพ
Add a duplicate option
แก้ไขไฟล์ดังนี้
openacademy/models.py
session_ids = fields.One2many( 'openacademy.session', 'course_id', string="Sessions") @api.multi def copy(self, default=None): default = dict(default or {}) copied_count = self.search_count( [('name', '=like', u"Copy of {}%".format(self.name))]) if not copied_count: new_name = u"Copy of {}".format(self.name) else: new_name = u"Copy of {} ({})".format(self.name, copied_count) default['name'] = new_name return super(Course, self).copy(default) _sql_constraints = [ ('name_description_check', 'CHECK(name != description)',
เพิ่ม option duplicate ให้กับ course ดังภาพ
และเมื่อคลิกที่ duplicate ก็จะทำการสร้าง course ใหม่ ชื่อว่า Copy of My First Course ดังภาพ
หัวข้อถัดไป Tutorial Odoo 9.0 Part 9 :