def relation_hack(form, fields, prefix=''):
    opts = form.instance._meta
    localm2m = [m2m.attname for m2m in opts.local_many_to_many]
    externalfk = [obj.field.related_query_name() for obj in opts.get_all_related_objects()]
    externalm2m = [m2m.get_accessor_name() for m2m in opts.get_all_related_many_to_many_objects()]
    for name, db_field in fields:
        full_name = '%s%s' % (prefix, name)
        if full_name in form.fields:
            value = getattr(form.instance, name, '')
            # check for (local) ForeignKeys
            if isinstance(value, Model):
                value = value.pk
            # check for (local) many to many fields
            elif name in localm2m:
                value = value.all()
            # check for (external) ForeignKeys
            elif name in externalfk:
                value = value.all()
            # check for (external) many to many fields
            elif name in externalm2m:
                value = value.all()
            form.fields[full_name].initial = value
dataset/ETHPy150Open hzlf/openbroadcast/website/multilingual/admin.py/relation_hack/original