'Get implicit defaults for new component records
@param master: the master record
@param defaults: any explicit defaults
@param data: any actual values for the new record
@return: a dict of {fieldname: values} with the defaults'

def get_defaults(self, master, defaults=None, data=None):

    values = {}
    parent = self.parent
    if (not parent):
        return values
    hook = current.s3db.get_component(parent.tablename, self.alias)
    filterby = hook.get('filterby')
    if filterby:
        for (k, v) in filterby.items():
            if (not isinstance(v, (tuple, list))):
                values[k] = v
    if self.defaults:
        values.update(self.defaults)
    if defaults:
        values.update(defaults)
    if data:
        values.update(data)
    lookup = {}
    for (k, v) in list(values.items()):
        if (k not in self.fields):
            del values[k]
            continue
        if isinstance(v, FS):
            try:
                rfield = v.resolve(parent)
            except (AttributeError, SyntaxError):
                continue
            field = rfield.field
            if ((not field) or (field.table != parent.table)):
                continue
            if (field.name in master):
                values[k] = master[field.name]
            else:
                del values[k]
                lookup[field.name] = k
    if lookup:
        row = None
        parent_id = parent._id
        record_id = master.get(parent_id.name)
        if record_id:
            fields = [parent.table[f] for f in lookup]
            row = current.db((parent_id == record_id)).select(limitby=(0, 1), *fields).first()
        if row:
            for (k, v) in lookup.items():
                if (k in row):
                    values[v] = row[k]
    return values
