sqlalchemy: Viele objecte zu einer m-to-m relation
Verfasst: Donnerstag 11. Dezember 2008, 01:50
Hi,
ich hab folgende (noch) kleine klasse die zu sqlalchemy gemappt wird..
da wo # add all tags to the entry steht... sollte genau das passieren... nur hab ich in den Docs zu sqlalchemy nichts gefunden wie ich das machen kann... evl. hat ja jemand von euch einen Tipp
gruessle zero-one
ich hab folgende (noch) kleine klasse die zu sqlalchemy gemappt wird..
Code: Alles auswählen
class Tag(object):
def __init__(self, name):
self.name = name
@staticmethod
def add_tags_by_string(tag_string, entry):
# check for tag separator..
split_char = ',' if ',' in tag_string else ' '
tags = [ tag.strip() for tag in tag_string.split(split_char) ]
# check for already existing tags...
existant_tags = session.query(Tag).filter(Tag.name.in_(tags)).all()
existant_tag_names = [ tag.name for tag in existant_tags ]
not_exitant_tags = [ Tag(tag) for tag in tags if tag not in existant_tag_names ]
# create not existant tags...
session.add_all(not_exitant_tags)
session.flush()
# add all tags to the entry...
gruessle zero-one