Create version and put it in a Playlist

Hi,
I’m currently using the API to create a version, now I need to put that version into a playlist or create a new playlist. How can i get a list of the existing playlists? I don’t see much in the api docs.

You may find going through this exercise helpful:

https://developers.shotgridsoftware.com/python-api/cookbook/tutorials.html#basic

But in general, you can do this:

Find existing playlists for the current project
Assuming 200 is the project id.

from pprint import pprint

playlists = sg.find("Playlist", filters=[["project.Project.id", "is", 200]], fields=["code"])

pprint(playlists)

This should print a list of playlist entities (dictionaries).
The Sg api will always return the type and id even if you didnt ask for those fields.

You need the type and id as minimum to update a field with an entity connection (single or multi entity fields).

Update the Version to add it to the Playlist
Assuming version is your version dictionary you just created

update_data = {
    "playlists": [playlist]
}
sg.update("Version", version.get("id"), update_data)

You can also update the Playlist instead and add the version to its list of versions (field).

In both cases you may need to use multi_entity_update_modes to ensure you don’t overwrite the playlists field with just the version. You likely want to add it.

update_data = {
    "playlists": [version]
}
sg.update("Version", version.get("id"), update_data, multi_entity_update_modes={"playlists": "add"})
2 Likes

Thank you for the tips. I’m getting a list of my playlists now, but once I find a single playlist that I want to add my version to, how would I do that? How does the above code know which playlist to add the version to or is it adding the version to all playlists?

Here is a dictionary of a single playlist that I would find:

{'type': 'Playlist', 'id': 14040, 'code': '20241202_animDAILIES'}

(In my previous post I had mistakenly put the version in the playlists field. I have corrected the code.)

You can do it in two ways, why you pick one over the other has to do with the logic of your code and what is more convenient at that time (and waht context you are dealing with).

In my example below we are updating the Version.
You can also update the Playlist and add the Version to it’s Versions field.
Since it’s a relational database there is a link between the two.

playlist = {"type": "Playlist", "id": 100}

update_data = {
    "playlists": [playlist]
}
sg.update("Version", version.get("id"), update_data, multi_entity_update_modes={"playlists": "add"})

playlist is my playlist, you can find it using the api, similar to finding a version.

update_data is a simple dictionary that will hold the information I want to update on the Version.

In this case I want to add my version to a playlist.

To do this I have to add the playlist to the Version’s playlists field (which is the codename of the Playlists field on the Version entity.

Similar how you would do it manualy (altough there is also a right click menu for this, you can in fact just edit the Playlists field and add the playlist of choice in the Web UI).

sg.update("Version", version.get("id"), update_data, multi_entity_update_modes={"playlists": "add"})

Here I’m telling the api to update a Version record with my Version’s id, use the update_data dictionary with information

I’m also adding multi_entity_update_modes and telling the api to just add the value of the playlists field in my data to what is already there. If I wouldnt do this I would overwrite it.

1 Like

YES! Thanks for the clarification. I’m now able to update my version and put it in a playlist.

1 Like