Returning fields other than just {'type','id','name'} from links?

Hi, I bet this question has been asked for years, but I’m gonna ask it again…

When the API returns the results for a single or multi-entity field, like say the “Link” i.e. entity or “Assigned To” i.e. task_assignees fields from a Task, the result always takes the form of a dictionary with three keys {'type', 'id', 'name'}. The 'name' is the normalized display_name_cache of the type/id’d entity. So the question is, is there any way to alter that set of default keys? For example, we virtually always want the 'login' for each of the folks in a Task’s task_assignees, but you can’t flow-thru / dot-syntax on multi-entity fields, so we have to run another query.

Secret black magic ruby hacks are welcome… :wink:

5 Likes

Mmm… I’ve wondered about this too… seems like there is no option but to run another query…

Here’s what the docs have to say…

http://developer.shotgunsoftware.com/python-api/usage_tips.html

This post seems to address this problem… still requires a second query but you can get the users login deets in one go

https://groups.google.com/a/shotgunsoftware.com/forum/#!topic/shotgun-dev/Pr5hdzsH6Ss

2 Likes

Hi, Tony and Adrian :wave:

Thanks for the question! Unfortunately, you’re right there’s no other way to do this :slightly_frowning_face: I double checked with our engineers to be sure as well. You can drop in a feature request for us on our Shotgun Roadmap link and share more details to our product team. Sorry we don’t have a better answer on this.

Best,
Tram

3 Likes

When the API is used directly, with no additional “fields” requested from the call, it will typically return only the 'type' and 'id' fields.
Eg: {'type': 'Asset', 'id': 12345}

I believe this is due to the concrete consistency of fields available across all Entity types in Shotgun.
They will always have a 'type' and an 'id' - these are used together to target a unique record in the database.

One way you can ‘adjust’ the return is to just create an access layer which wraps the default SG.find() / SG.find_one() methods of the shotgun python api to extend the filters argument for the field names you want to return.

# example pseudo-code
# file: shotgun.py (sourced) - python 2.7
import shotgun_api3

class Shotgun(shotgun_api3.Shotgun):
    def find(entity_type, filters, fields=None, **kwargs):
        fields = fields or list()
        if entity_type == 'Task':
            fields.extend(['name', 'custom_field'])
        super(Shotgun, self).find(entity_type, filters, fields, **kwargs)

    def find_one(entity_type, filters, fields=None, **kwargs):
        fields = fields or list()
        if entity_type == 'Task':
            fields.extend(['name', 'custom_field'])
        super(Shotgun, self).find_one(entity_type, filters, fields, **kwargs)

To use, you would just source this module rather than the original shotgun_api3 module.

import shotgun
SG = shotgun.Shotgun(SERVER_PATH, SCRIPT_NAME, SCRIPT_KEY)
SG.find_one('Task', [], [])
# >>> {'type': 'Task', 'id': 1234, 'name': 'Animation', 'custom_field': 'your data'}

It’s much easier to extend for future needs as well - especially things which may be unique fields to your studio instance of Shotgun.

1 Like