'Removes optional columns like `_user_tags`, `_comments` etc. if not in table'

def set_optional_columns(self):

    columns = frappe.db.get_table_columns(self.doctype)
    to_remove = []
    for fld in self.fields:
        for f in optional_fields:
            if ((f in fld) and (not (f in columns))):
                to_remove.append(fld)
    for fld in to_remove:
        del self.fields[self.fields.index(fld)]
    to_remove = []
    for each in self.filters:
        if isinstance(each, basestring):
            each = [each]
        for element in each:
            if ((element in optional_fields) and (element not in columns)):
                to_remove.append(each)
    for each in to_remove:
        if isinstance(self.filters, dict):
            del self.filters[each]
        else:
            self.filters.remove(each)
