@api_endpoint
@ajax_request
@login_required
def create_sub_org(request):
    body = json.loads(request.body)
    org = Organization.objects.get(pk=body['parent_org_id'])
    email = body['sub_org']['email']
    try:
        user = User.objects.get(username=email)
    except User.DoesNotExist:
        return {
            'status': 'error',
            'message': 'User with email address (%s) does not exist' % email
        }
    sub_org = Organization.objects.create(
        name=body['sub_org']['name']
    )

    OrganizationUser.objects.get_or_create(user=user, organization=sub_org)

    sub_org.parent_org = org

    try:
        sub_org.save()
    except TooManyNestedOrgs:
        sub_org.delete()
        return {
            'status': 'error',
            'message': 'Tried to create child of a child organization.'
        }

    return {'status': 'success',
            'organization_id': sub_org.pk}
Creates a child org of a parent org.
dataset/ETHPy150Open buildingenergy/buildingenergy-platform/seed/views/accounts.py/create_sub_org