dabapps / django-rest-framework-serialization-spec

DEPRECATED, see https://github.com/dabapps/django-readers instead
MIT License
11 stars 0 forks source link

Try using values_list() and manual stitching to create nested data outputs #49

Open pmg103 opened 4 years ago

pmg103 commented 4 years ago

Spike to test out this idea: https://github.com/dabapps/django-rest-framework-serialization-spec/issues/47

In [1]: from project.assets.models.activities import Activity
   ...: from serialization import serializej                                                                                                                               

In [2]: qs = Activity.objects.all()[:2]                                                                                                                                                 

In [3]: serializej(qs, ['name'])                                                                                                                                                        
[
  {
    "name": "360 Leading leaders"
  },
  {
    "name": "360 Leading self"
  }
]

In [4]: serializej(qs, ['users'])                                                                                                                                                       
[
  {
    "users": [
      "06a25509-280c-44bd-ad79-b366228c9cbf",
      "e49ae9a2-78b2-47be-b023-ec6bb0d1e1c5",
      "09e5f7dd-ca56-4421-acb2-e586eaa06bd7"
    ]
  },
  {
    "users": [
      "3ba874d0-c290-4048-abdf-d5869dbdce4a"
    ]
  }
]

In [14]: serializej(qs, [ 
    ...:   ('users', [ 
    ...:     ('user', ['full_name', 'email']), 
    ...:   ]), 
    ...: ])                                                                                                                                                                             
[
  {
    "users": [
      {
        "user": {
          "full_name": "Callan Patel",
          "email": "callan@sp27payg.com"
        }
      },
      {
        "user": {
          "full_name": "Can Waters",
          "email": "can@sp27payg.com"
        }
      },
      {
        "user": {
          "full_name": "Cara Craft",
          "email": "cara@sp27payg.com"
        }
      }
    ]
  },
  {
    "users": [
      {
        "user": {
          "full_name": "Bradlee Hollis",
          "email": "bradlee@sp27payg.com"
        }
      }
    ]
  }
]

In [6]: serializej(qs, [ 
   ...:   ('users', [ 
   ...:     ('user', ['full_name', 'email']), 
   ...:   ]), 
   ...:   ('product_versions', [ 
   ...:     ('product', ['name', 'type']) 
   ...:   ]), 
   ...:   'name', 
   ...:   ('organisation', [ 
   ...:     'name', 
   ...:     ('created_by_user', ['id', 'email']) 
   ...:   ]) 
   ...: ])                                                                                                                                                                              
[
  {
    "users": [
      {
        "user": {
          "email": "callan@sp27payg.com",
          "full_name": "Callan Patel"
        }
      },
      {
        "user": {
          "email": "can@sp27payg.com",
          "full_name": "Can Waters"
        }
      },
      {
        "user": {
          "email": "cara@sp27payg.com",
          "full_name": "Cara Craft"
        }
      }
    ],
    "organisation": {
      "created_by_user": {
        "email": "kim.downes@peoplewise.co.uk",
        "id": "141c97e0-9a88-491c-b98d-f3dfb21a2746"
      },
      "name": "27 PAYG"
    },
    "product_versions": [
      {
        "product": {
          "type": "THREE_SIXTY",
          "name": "PENSPEN 360 Lite"
        }
      }
    ],
    "name": "360 Leading leaders"
  },
  {
    "users": [
      {
        "user": {
          "email": "bradlee@sp27payg.com",
          "full_name": "Bradlee Hollis"
        }
      }
    ],
    "organisation": {
      "created_by_user": {
        "email": "kim.downes@peoplewise.co.uk",
        "id": "141c97e0-9a88-491c-b98d-f3dfb21a2746"
      },
      "name": "27 PAYG"
    },
    "product_versions": [
      {
        "product": {
          "type": "THREE_SIXTY",
          "name": "PENSPEN 360 Lite"
        }
      }
    ],
    "name": "360 Leading self"
  }
]