У меня есть вопрос относительно SQLAlchemy. Как добавить в свой сопоставленный класс словарь-подобный атрибут, который отображает строковые ключи в строковые значения и который будет храниться в базе данных (в той же или другой таблице, что и исходный сопоставленный объект). Я хочу, чтобы это добавляло поддержку для произвольных тегов моих объектов.
Я нашел следующий пример в документации SQLAlchemy:
from sqlalchemy.orm.collections import column_mapped_collection, attribute_mapped_collection, mapped_collection
mapper(Item, items_table, properties={
# key by column
'notes': relation(Note, collection_class=column_mapped_collection(notes_table.c.keyword)),
# or named attribute
'notes2': relation(Note, collection_class=attribute_mapped_collection('keyword')),
# or any callable
'notes3': relation(Note, collection_class=mapped_collection(lambda entity: entity.a + entity.b))
})
item = Item()
item.notes['color'] = Note('color', 'blue')
Но мне нужно следующее поведение:
mapper(Item, items_table, properties={
# key by column
'notes': relation(...),
})
item = Item()
item.notes['color'] = 'blue'
В SQLAlchemy возможно?
Спасибо