From 7957dff33f8d3d8d26360df999817899e53ce86f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 11 Apr 2013 14:23:06 +0900 Subject: [PATCH] make sure sort field updated when field deleted (#737) --- anki/models.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/anki/models.py b/anki/models.py index 0099422bf..4eaf28d58 100644 --- a/anki/models.py +++ b/anki/models.py @@ -250,17 +250,23 @@ and notes.mid = ? and cards.ord = ?""", m['id'], ord) def remField(self, m, field): self.col.modSchema() + # save old sort field + sortFldName = m['flds'][m['sortf']]['name'] idx = m['flds'].index(field) m['flds'].remove(field) - if m['sortf'] >= len(m['flds']): - m['sortf'] -= 1 + # restore old sort field if possible, or revert to first field + m['sortf'] = 0 + for c, f in enumerate(m['flds']): + if f['name'] == sortFldName: + m['sortf'] = c + break self._updateFieldOrds(m) def delete(fields): del fields[idx] return fields self._transformFields(m, delete) - if idx == self.sortIdx(m): - # need to rebuild + if m['flds'][m['sortf']]['name'] != sortFldName: + # need to rebuild sort field self.col.updateFieldCache(self.nids(m)) # saves self.renameField(m, field, None)