66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
from odoo import fields, models, api
|
||
|
||
|
||
class ForumPost(models.Model):
|
||
_name = 'yuthon.school.forum_post'
|
||
_description = '学习交流'
|
||
_order = 'create_date desc, id'
|
||
|
||
name = fields.Char(
|
||
string='主题',
|
||
required=True,
|
||
help='如:Many2many 的 (6,0,[ids]) 怎么理解?',
|
||
)
|
||
content = fields.Html(
|
||
string='内容',
|
||
sanitize=True,
|
||
)
|
||
author_name = fields.Char(
|
||
string='作者',
|
||
help='发帖人姓名(教学演示用,避免关联复杂权限)',
|
||
)
|
||
author_type = fields.Selection(
|
||
[('student', '学生'), ('teacher', '老师')],
|
||
string='作者类型',
|
||
default='student',
|
||
)
|
||
course_id = fields.Many2one(
|
||
'yuthon.school.course',
|
||
string='关联课程',
|
||
ondelete='restrict',
|
||
)
|
||
post_type = fields.Selection(
|
||
[('question', '提问'), ('share', '分享'), ('discuss', '讨论')],
|
||
string='类型',
|
||
default='question',
|
||
)
|
||
like_count = fields.Integer(
|
||
string='点赞数',
|
||
default=0,
|
||
)
|
||
parent_id = fields.Many2one(
|
||
'yuthon.school.forum_post',
|
||
string='回复对象',
|
||
ondelete='cascade',
|
||
help='非空表示这是某帖的回复(实现楼中楼)',
|
||
)
|
||
reply_ids = fields.One2many(
|
||
'yuthon.school.forum_post',
|
||
'parent_id',
|
||
string='回复',
|
||
)
|
||
reply_count = fields.Integer(
|
||
string='回复数',
|
||
compute='_compute_reply_count',
|
||
store=True,
|
||
)
|
||
active = fields.Boolean(
|
||
string='启用',
|
||
default=True,
|
||
)
|
||
|
||
@api.depends('reply_ids')
|
||
def _compute_reply_count(self):
|
||
for record in self:
|
||
record.reply_count = len(record.reply_ids)
|