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!

3 Likes

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.

8 Likes

This is great, thanks @taiello

1 Like

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.

3 Likes

Can we have a copy of the code above please? Somehow I am not getting the result as expected.

@taiello is there anything you can provide more specifically? I’m a little confused here. Thanks a ton in advance!

Having to use the schema of the query and feed it to a find/summarize is more of a workaround.

I feel the expected behavior of a simple sg.find_one('Shot', [], ['my_query_field']) would return the value. Returning “None” is completely pointless.

Hey everyone!

I had to build a solution to this problem with a project that I was working on recently. I remembered coming accross this thread and thought I would share what I came up with. I did my best to package up the functionality that I thought would be helpful to other developers.

https://github.com/mbsurfer/shotgun-extensions

Importing this package will give you access to new versions of the find and find_one methods you would traditionally use with the ShotGun class.

How it works

After executing the relative find method, I check if any of the fileds requested are query fields. I then use the entity schema to construct additional queries and return the query values with the rest of the results. My goal was to make using these find functions as seamless as possible with the way they work now.

Expect these new methods to be slower than the standard find methods because of all the extra queries that need to be made, so please keep that in mind.

Feel free to reach out to me if you have any questions or suggestions.

1 Like

Hey @rneldner this is great thanks for sharing!

Semi-related, have you at all attempted to pull data from PageSettings to inspect filters in the web and then attempted to translate them at all? I haven’t done this myself but this repo is a good excuse to add this as another feature. Will let you know if I fork this and get something working but it won’t be anytime soon unfortunately…!

You bet!

Unfortunately, I haven’t had to deal with pages as all, but I am curious on how similar the process might be. I was juest testing requesting a few pages directly from the API and I am not seeing any filter information being retruned. Have you been able to retrieve that info?

I am using the GET /api/v1/entity/pages/:id and the GET /schema/pages/fields routes.