<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def ko_data(queryset, field_names=None, name=None, safe=False, return_json=False):
 """ Given a QuerySet, return just the serialized representation based on the knockout_fields as JavaScript. """


    try:
        try:
            # Get an inital instance of the QS.
            queryset_instance = queryset[0]
        except TypeError as e:
            # We are being passed an object rather than a QuerySet.
            # That's naughty, but we'll survive.
            queryset_instance = queryset
            queryset = [queryset]
        except IndexError as e:
            if not isinstance(queryset, list):
                # This is an empty QS - get the model directly.
                queryset_instance = queryset.model
            else:
                # We have been given an empty list.
                # Return nothing.
                return '[]'

        modelName = queryset_instance.__class__.__name__
        modelNameData = []

        if field_names is not None:
            fields = field_names
        else:
            fields = get_fields(queryset_instance)

        for obj in queryset:
            object_data = get_object_data(obj, fields, safe)
            modelNameData.append(object_data)

        if name:
            modelNameString = name
        else:
            modelNameString = modelName + "Data"

        dthandler = lambda obj: obj.isoformat() if isinstance(obj, (datetime.date, datetime.datetime)) else None
        dumped_json = json.dumps(modelNameData, default=dthandler)

        if return_json:
            return dumped_json
        return "var " + modelNameString + " = " + dumped_json + ';'
    except Exception as e:
        logger.exception(e)
        return '[]'