Skip to content

Commit

Permalink
Merge pull request #37 from Yifchan/master
Browse files Browse the repository at this point in the history
[FIX]修复第四章计算字段在循环时误用self导致不能添加多条记录的问题
  • Loading branch information
ruter authored Mar 31, 2020
2 parents 9b0ff16 + 9a526d3 commit 2851d10
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Chapter-4/Computed-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ class Bangumi(models.Model):
today = fields.Date.today()
dt = today - record.release_date
if dt.days < 0:
self.current = 0
record.current = 0
continue

if record.update_cycle == 'weekly':
current = round(dt.days / 7)
self.current = current if current < record.total else record.total
record.current = current if current < record.total else record.total

if record.update_cycle == 'monthly':
month_dt = (today.year - record.release_date.year) * 12 + (today.month - record.release_date.month)
self.current = month_dt if month_dt < record.total else record.total
record.current = month_dt if month_dt < record.total else record.total

if record.update_cycle == 'quarterly':
quarter_dt = (today.year - record.release_date.year) * 4 + (
math.ceil(today.month / 3) - math.ceil(record.release_date.month / 3))
self.current = quarter_dt if quarter_dt < record.total else record.total
record.current = quarter_dt if quarter_dt < record.total else record.total

name = fields.Char(string='Name', required=True)
current = fields.Integer(string='Current', compute='_compute_current')
Expand Down

0 comments on commit 2851d10

Please sign in to comment.