'Post-processes this subform element against the POST data,
and create/update/delete any related records.
@param form: the master form
@param master_id: the ID of the master record in the form
@param format: the data format extension (for audit)
@todo: implement audit'

def accept(self, form, master_id=None, format=None):

    s3db = current.s3db
    fname = self._formname(separator='_')
    resource = self.resource
    success = False
    if (fname in form.vars):
        values = form.vars[fname]
        if (values is None):
            values = []
        elif (not isinstance(values, (list, tuple, set))):
            values = [values]
        values = set((str(v) for v in values))
        (component, link) = self.get_link()
        pkey = component.pkey
        if (pkey == resource._id.name):
            master = {pkey: master_id}
        else:
            query = (resource._id == master_id)
            master = current.db(query).select(resource.table[pkey], limitby=(0, 1)).first()
        if master:
            query = (FS(component.lkey) == master[pkey])
            lresource = s3db.resource(link.tablename, filter=query)
            rows = lresource.select([component.rkey], as_rows=True)
            if rows:
                rkey = link.table[component.rkey]
                current_ids = set((str(row[rkey]) for row in rows))
                delete = (current_ids - values)
                insert = (values - current_ids)
            else:
                delete = None
                insert = values
            if delete:
                query &= FS(component.rkey).belongs(delete)
                lresource = s3db.resource(link.tablename, filter=query)
                lresource.delete()
            insert.discard('')
            if insert:
                for record_id in insert:
                    record = {component.fkey: record_id}
                    link.update_link(master, record)
            success = True
    return success
