JSON to model a class using Django

前端 未结 3 412
难免孤独
难免孤独 2020-12-31 17:36

I\'m trying to get a JSON object like:

{
    \"username\": \"clelio\",
    \"name\": \"Clelio de Paula\",
}

and transform it in:

         


        
3条回答
  •  梦谈多话
    2020-12-31 18:11

    You probably want to look at Django's (de)serialization framework. Given JSON like:

    [
      {
        "model": "myapp.user",
        "pk": "89900",
        "fields": {
          "name": "Clelio de Paula"
        }
      }
    ]
    

    you can save it like this:

    from django.core import serializers
    for deserialized_object in serializers.deserialize("json", data):
        deserialized_object.save()
    

    Note that I believe you have to use the Django serialization format to use this method, so you might have to adjust your JSON accordingly.

提交回复
热议问题