'Set title field based on template'

def set_title_field(self):

    def get_values():
        values = self.as_dict()
        for (key, value) in iteritems(values):
            if (value == None):
                values[key] = u''
        return values
    if (self.meta.get(u'title_field') == u'title'):
        df = self.meta.get_field(self.meta.title_field)
        if df.options:
            self.set(df.fieldname, df.options.format(**get_values()))
        elif (self.is_new() and (not self.get(df.fieldname)) and df.default):
            self.set(df.fieldname, df.default.format(**get_values()))
