class ForeignKeyFormMixin(object):

    def __init__(self, *args, **kwargs):
        super(ForeignKeyFormMixin, self).__init__(*args, **kwargs)
        for k, v in self.foreign_keys.items():
            f = collections.OrderedDict()
            for kk, vv in v().fields.items():
                self.fields['{}_{}'.format(k, kk)] = vv
                if not self.fields['{}_{}'.format(k, kk)].label:
                    self.fields['{}_{}'.format(k, kk)].label = kk.replace(
                        '_', ' ').title()
                f['{}_{}'.format(k, kk)] = self.fields['{}_{}'.format(k, kk)]

            v_form = v(*args, **kwargs)
            v_form.fields = f
            setattr(self, k, v_form)