'Returns a Form class for use in the admin add view. This is used by
add_view and change_view.'

def get_form(self, request, obj=None, **kwargs):

    parent_id = request.GET.get(u'parent_id', None)
    if (not parent_id):
        parent_id = request.POST.get(u'parent_id', None)
    if parent_id:
        return AddFolderPopupForm
    else:
        folder_form = super(FolderAdmin, self).get_form(request, obj=None, **kwargs)
        def folder_form_clean(form_obj):
            cleaned_data = form_obj.cleaned_data
            folders_with_same_name = Folder.objects.filter(parent=form_obj.instance.parent, name=cleaned_data[u'name'])
            if form_obj.instance.pk:
                folders_with_same_name = folders_with_same_name.exclude(pk=form_obj.instance.pk)
            if folders_with_same_name.exists():
                raise ValidationError(u'Folder    with    this    name    already    exists.')
            return cleaned_data
        folder_form.clean = folder_form_clean
        return folder_form
