API to get query field values

Hello,

I’ve got a custom query type field created for the Shot entity. When I try to get the value of that field using the Shotgun Python API (shotgun.find_one with the field name in the fields argument), it always comes back as None, even though it has a value on the Shotgun site. Is fetching query field values possible and if so - how is it done?

Thanks!

Yes, you can achieve this.

First, be sure to use the right field name:
On a shot page, display the searched column, right clic on the header, configure field and there you’ll see the Field code. It’s usually different from the displayed name.

Then you can query this field like any other.

If this doesn’t help, please post your code. It will be easier to spot the issue.

Cheers

1 Like

Hi @Kevin_Lmr

Thanks for your reply. What you’re describing doesn’t seem to work for me.

Here’s what I see on our Shotgun site:

And here’s my Python code:

    shot = sg.find_one('Shot',
                       filters=[['id', 'is', shot_id]],
                       fields=['sg_latest_version_for_delivery'])
    print(shot['sg_latest_version_for_delivery'])

This returns None.

And what do you get if you print(shot) ?

This:

OK, just wanted to be sure that your query was working.
And I assume you’ve double checked the shot id.

Does the authenticated user in the script can read this field?

Yes, it’s the same shot. Actually, reading query fields on any shot returns None for me.

I’m using an API Admin script to get the Shotgun instance that I’m using to fetch the shot, so it should be able to see everything.

Oh ok, sorry. I didn’t read the question properly then. I skip the “query” field part.
Indeed, it’s also returning None on my test then.

Could you then post your query? How do you filter last version?
Maybe we can achieve this with a more complete filter.

This is my query:

If you’re referring to re-creating the query for the version using the Python API - I already figured I could do that, but it’s just an extra step. I was hoping could be avoided, since the field on Shotgun should already contain a value or at least I hoped it could be forced to calculate the value for you in the response.

OK, then, there is nothing else I can do to help :wink:

From the documentation:

Shotgun UI fields not available via the API

Summary type fields like Query Fields and Pipeline Step summary fields are currently only available via the UI. Some other fields may not work as expected through the API because they are “display only” fields made available for convenience and are only available in the browser UI.

1 Like

You can retrieve the configuration of a query field via the API using sg.schema_field_read and then pass that config into an sg.find or sg.summarize call.

In your case, you’d do:
my_field_config = sg.schema_field_read('Shot', 'sg_latest_version_for_delivery')['sg_latest_version_for_delivery']

Then the configuration of the query field, including the filters and the intended format of the result, will be available as a dict in:
my_field_config['properties']

You’d then need to pop keys out of that result and stuff them into an sg.summarize() call.

This general approach avoids having to duplicate your query config in your client code, allowing you to reconfigure the query field in the UI and have your code continue to work with it without having to refactor.

6 Likes

This is great, thanks @taiello

Is there an example for the final code? I am trying to do the exact same thing but am getting a bit lost in the return value of the schema.