我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用django.core.serializers.get_serializer()。
def getShortestPath(request): # Task List as a dict object taskList = json.loads(request.POST["taskList"]) # Build the list of task descriptions (semicolon separated). E.g: "B;D;E" taskSelectionString = ";".join("{0}{1}".format(t["description"], "") for t in taskList) # Get the Shortest Path for the specified tasks - String of semicolon separated task descritpions. E.g: "D;B;E" shortestPath = ShortestPath.objects.filter(task_selection = taskSelectionString)[0] # Build the actual list of Task objects forming the shortest path. E.g: [<Task: D>, <Task: B>, <Task: E>] shortestPathTasks = shortestPath.getShortestPathTasks() # Serialize and return the list of tasks as JSON json_serializer = serializers.get_serializer("json")() shortestPathJson = json_serializer.serialize(shortestPathTasks) logger = logging.getLogger("console_logger") return HttpResponse(json.dumps({"shortestPath": shortestPathJson, "totalCost": shortestPath.total_cost, "totalReward": shortestPath.total_reward}), content_type="application_json")
def get_locations(request): """ Returns a JSON string with the processed locations for all the entries available in the database. Calling this function will update the entries with its corresponding location. """ # Update all Entry Locations update_entry_locations(Entry.objects.all()) # Get locations locations = Location.objects.all() # Serialize and render JSONSerializer = serializers.get_serializer("json") json_serializer = JSONSerializer() try: # Discriminate duplicates if possible json_serializer.serialize(locations.distinct('longitude','latitude')) except NotImplementedError: # If not render all json_serializer.serialize(locations) data = json_serializer.getvalue() return HttpResponse(data,content_type="application/json")
def getTasksToPerform(request): json_serializer = serializers.get_serializer("json")() task_list = json_serializer.serialize(request.user.getTasksToPerform()) return HttpResponse(task_list, content_type="application/json")
def findOptimalTaskSet(request): availableEnergy = request.user.energy_units - int(request.POST["energyToSpare"]) taskList = json.loads(request.POST["taskList"]) taskSelectionString = ";".join("{0}{1}".format(t["description"], "") for t in taskList) optimalPath = ShortestPath.findOptimalPath(taskSelectionString, availableEnergy) optimalPathTasks = optimalPath.getShortestPathTasks() json_serializer = serializers.get_serializer("json")() optimalPathTasksJson = json_serializer.serialize(optimalPathTasks) return HttpResponse(json.dumps({"shortestPath": optimalPathTasksJson, "totalCost": optimalPath.total_cost, "totalReward": optimalPath.total_reward}), content_type="application_json")
def create_serializer(cls, format): '''create instance of serializer from two classes''' try: FormatSerializerCls = serializers.get_serializer(format) except serializers.SerializerDoesNotExist: raise Exception("Unknown serialization format: %s" % format) SerializerCls = type('GitVersionSerializer', ( cls, FormatSerializerCls), {}) return SerializerCls()
def test_dump(self): """ Basic assignment """ from django.core import serializers JSSerializer = serializers.get_serializer('json') js_serializer = JSSerializer() js = js_serializer.serialize(DummyModel.objects.all()) js_des = json.loads(js) des_obj = js_des[0] self.assertEqual(des_obj['pk'], 1) self.assertEqual(json.loads(des_obj['fields']['field']), {'key': 'value', })
def dump_data(self): """ ?????????? ??????? main.Char ?? ?????? ??????? ??????? """ json_serializer = serializers.get_serializer('json') json_dumper = json_serializer() _models = [ (Author, 'authors'), (Book, 'book'), (Sequence, 'sequence'), (SequenceBook, 'sequencebook'), (Genre, 'genre'), (Language, 'language'), (Translator, 'translator'), (Char, 'char'), (MenuItem, 'menuitem'), (Publisher, 'publisher'), (Site, 'site') ] for model, name in _models: with open('%s.json' % name, 'w') as out: json_dumper.serialize(model.objects.all(), stream=out)