Jump to content

Query project by name (string) with latest API?


Mike

Recommended Posts

Posted

1) I'm having trouble getting the criteria system to work. 

When the tool starts up, in the main window module queries all of the active projects and populates a QTreeView:

session = ftrack_api.Session()
 
dir_contents = []
for project in session.query('Project'):
    dir_contents.append( project )

When a user selects a project to work with, another module creates a tab and queries the project data- populating this new tab with directory hierarchy, tasks, thumbnails, notes, etc.  I was able to begin querying a project's ftrack data by using the legacy method getProject('string'), but I'm having trouble getting the equivalent to work using the latest API.  This is what I have right now:

selected_index = self.tab_projects.tv_projects_view.selectedIndexes()[0]
session = ftrack_api.Session()
 
project = session.query(
    'Project where name is "{0}"'
    .format(selected_index.data())
)

The value for selected_index.data() is just a string like 'MY_PROJECT_NAME'.

I keep getting this error: QueryResult.__getitem__ in query line 89: list indices must be integers, not str.

2) Also, if I'm querying, should I be setting a session variable in every module that queries, or should I be doing this globally somewhere?

Thanks!

----------------EDIT-------------

3) So I figured out from here that I needed to add .one() at the end of the query like this.  I guess it's a python method to only return 1 value.  Why wouldn't this query return just 1 value by default though?  

project = self.session.query(
    'Project where name is {0}'     
    .format(selected_index.data())
).one()

 

Posted

It does return only 1 value, however it's wrapped in a `QueryResult` object which is technically a list. `.one()` ensures that there was actually only one result of the query and returns it, otherwise it fails. Say you just ask for a shot named `sh010`, there's a big chance you'll have the same shot name in another project so the query would have multiple results, hence raising and error when asking for just one.

 

Have a look at the API reference here Query API ref

Posted
15 hours ago, Mike said:

The value for selected_index.data() is just a string like 'MY_PROJECT_NAME'.

I keep getting this error: QueryResult.__getitem__ in query line 89: list indices must be integers, not str.

3) So I figured out from here that I needed to add .one() at the end of the query like this.  I guess it's a python method to only return 1 value.  Why wouldn't this query return just 1 value by default though?  

Yes, session.query() returns a ftrack_api.query.QueryResult. You can run .first(), .one() or .all() on it, or iterate over it as you do in one of your examples.

2) Also, if I'm querying, should I be setting a session variable in every module that queries, or should I be doing this globally somewhere?

This really depends on what type of application you build. Personally when I build ui components I'd like to send in the session to it when creating it, so if I reuse the same components elsewhere you can decide at that point which session you want to use. Be aware that there is a built in memory cache in the api so sharing sessions too much without reseting them may lead to stale data - on the other hand it helps performance.

project = session.query(
    'Project where name is "{0}"'
    .format(selected_index.data())
)

As a side note, consider selecting name and/or full_name to pre-load the data you need in the dropdown. This helps performance

Posted

Thank for the help everyone!

8 hours ago, Mattias Lagergren said:

project = session.query(
    'Project where name is "{0}"'
    .format(selected_index.data())
)

As a side note, consider selecting name and/or full_name to pre-load the data you need in the dropdown. This helps performance

Like this?

project = session.query(
    'Project where name is "{0}" or full_name is "{0}"'
    .format(selected_index.data())
)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...