docs: def save(self):
          '''
          Persist changes to rethinkdb. Updates only the fields that have
          changed. Performs insert rather than update if the document has no
          primary key or if the primary key is absent from the database.
  
          If there have been any changes to nested fields, updates the first
          level attribute. For example, if foo['bar']['baz']['quux'] has changed,
          all of foo['bar'] is replaced, but foo['something_else'] is not
          touched.
          '''
          should_insert = False
          try:
              self[self.pk_field]  # raises KeyError if missing
              if self._updates:
                  # r.literal() to replace, not merge with, nested fields
                  updates = {field: r.literal(self._updates[field])
                             for field in self._updates}
                  query = self.rr.table(self.table).get(
                          self.pk_value).update(updates)
                  result = query.run()
                  if result['skipped']:  # primary key not found
                      should_insert = True
                  elif result['errors'] or result['deleted']:
                      raise Exception(
                              'unexpected result %s from rethinkdb query %s' % (
                                  result, query))
              if not should_insert and self._deletes:
                  query = self.rr.table(self.table).get(self.pk_value).replace(
                          r.row.without(self._deletes))
                  result = query.run()
                  if result['errors']:   # primary key not found
                      should_insert = True
                  elif result['replaced'] != 1:
                      raise Exception(
                              'unexpected result %s from rethinkdb query %s' % (
                                  result, query))
          except KeyError:
              should_insert = True
  
          if should_insert:
              query = self.rr.table(self.table).insert(self)
              result = query.run()
              if result['inserted'] != 1:
                      raise Exception(
                              'unexpected result %s from rethinkdb query %s' % (
                                  result, query))
              if 'generated_keys' in result:
                  dict.__setitem__(
                          self, self.pk_field, result['generated_keys'][0])
  
          self._clear_updates()