Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pmg103
Copy link
Contributor

@pmg103 pmg103 commented Feb 6, 2020

Spike to test out this idea: #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": "[email protected]"
        }
      },
      {
        "user": {
          "full_name": "Can Waters",
          "email": "[email protected]"
        }
      },
      {
        "user": {
          "full_name": "Cara Craft",
          "email": "[email protected]"
        }
      }
    ]
  },
  {
    "users": [
      {
        "user": {
          "full_name": "Bradlee Hollis",
          "email": "[email protected]"
        }
      }
    ]
  }
]

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": "[email protected]",
          "full_name": "Callan Patel"
        }
      },
      {
        "user": {
          "email": "[email protected]",
          "full_name": "Can Waters"
        }
      },
      {
        "user": {
          "email": "[email protected]",
          "full_name": "Cara Craft"
        }
      }
    ],
    "organisation": {
      "created_by_user": {
        "email": "[email protected]",
        "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": "[email protected]",
          "full_name": "Bradlee Hollis"
        }
      }
    ],
    "organisation": {
      "created_by_user": {
        "email": "[email protected]",
        "id": "141c97e0-9a88-491c-b98d-f3dfb21a2746"
      },
      "name": "27 PAYG"
    },
    "product_versions": [
      {
        "product": {
          "type": "THREE_SIXTY",
          "name": "PENSPEN 360 Lite"
        }
      }
    ],
    "name": "360 Leading self"
  }
]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant