def __init__(self, user=None, contact_type=None, *args, **kwargs):
        "Populates form with fields from given ContactType"

        if 'instance' in kwargs:
            self.instance = kwargs['instance']
            values = self.instance.contactvalue_set.all()
            del kwargs['instance']

        super(ContactForm, self).__init__(*args, **kwargs)

        self.fields['parent'] = forms.ModelChoiceField(
            label='Parent', queryset=[], required=False)
        self.fields['parent'].queryset = Object.filter_permitted(
            user, Contact.objects)
        self.fields['parent'].widget.attrs.update({'class': 'autocomplete',
                                                   'callback': reverse('identities_ajax_contact_lookup')})
        self.fields['parent'].widget.attrs.update(
            {'popuplink': reverse('identities_contact_add')})
        self.fields['parent'].label = _('Parent')
        self.fields['name'].label = _('Name')

        if 'files' in kwargs:
            self.files = kwargs['files']

        for field in contact_type.fields.all():
            if self.instance:
                initial_field_name = self._get_free_field_name(field)
                self.fields[initial_field_name] = self._get_form_field(field)
                for value in values:
                    if value.field == field:
                        field_name = self._get_free_field_name(field)
                        self.fields[field_name] = self._get_form_field(
                            field, value)
                        if initial_field_name in self.fields:
                            del self.fields[initial_field_name]
            else:
                field_name = self._get_free_field_name(field)
                self.fields[field_name] = self._get_form_field(field)

        if user.is_admin('treeio.identities'):
            self.fields['related_user'] = forms.ModelChoiceField(label=_('Attach to User'),
                                                                 queryset=[], required=False)
            self.fields['related_user'].queryset = AccessEntity.objects.all()
            self.fields['related_user'].widget.attrs.update({'class': 'autocomplete',
                                                             'callback': reverse('identities_ajax_access_lookup')})
            self.fields['related_user'].label = _('Related user')

        if self.instance:
            self.fields['name'].initial = self.instance.name
            self.fields['parent'].initial = self.instance.parent_id
            if 'related_user' in self.fields:
                self.fields[
                    'related_user'].initial = self.instance.related_user_id
dataset/ETHPy150Open treeio/treeio/treeio/identities/forms.py/ContactForm.__init__/original