Open edX Opaque Keys

4.0.0 · active · verified Thu Apr 16

edx-opaque-keys is a Python library that provides a clear and consistent API for creating and introspecting identifiers (known as opaque keys) for various Open edX objects like courses and XBlocks. The library is currently at version 4.0.0 and is actively maintained with frequent updates, often related to Python requirements and dependency bumps.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create `CourseKey` and `UsageKey` objects from string identifiers and how to extract their components. It also shows how to generate a `UsageKey` from a `CourseKey` and convert keys back to their string representations. A conceptual example for Django model fields is also included.

from opaque_keys.edx.keys import CourseKey, UsageKey

# Create a CourseKey from a string identifier
course_id_string = "course-v1:OrgX+DemoCourse+2023_T1"
course_key = CourseKey.from_string(course_id_string)
print(f"Parsed CourseKey: {course_key}")
print(f"Organization: {course_key.org}")
print(f"Course: {course_key.course}")

# Create a UsageKey (for an XBlock) associated with the course
usage_key = course_key.make_usage_key("video", "video_id_123")
print(f"Generated UsageKey: {usage_key}")

# Convert key back to string
key_string = str(usage_key)
print(f"UsageKey as string: {key_string}")

# Example with Django model field (conceptual, requires Django setup)
try:
    from opaque_keys.edx.django.models import CourseKeyField
    # Example of how it would be used in a Django model
    # class MyModel(models.Model):
    #     course_id = CourseKeyField(max_length=255)
    print("Django model fields available for integration.")
except ImportError:
    print("Django is not installed or Django-specific features are not in use.")

view raw JSON →