Yo'av Moshe wrote:
> I'm using TG1 and I'm trying to create a tag cloud using Elixir
> without success.
> [snip]
> But using Elixir, I don't seem to be able to reach the table
> which holds the links between the items and the tags - the
> "pagetag_table".
You can access the secondary table as follows:
Tag.mapper.get_property('items').secondary
I would recommend against using the associable extension, and would
instead suggest using a more explicit association object pattern
that doesn't require use of the ManyToMany relation type, which in
my opinion isn't very easy to work with. Here is an example:
from elixir import *
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy import and_
class Item(Entity):
title = Field(Unicode(255))
tag_associations = OneToMany('TagAssociation')
tags = association_proxy(
'tag_associations', 'tag',
creator=lambda tag: TagAssociation(tag=tag)
)
class Tag(Entity):
name = Field(Unicode(255), unique=True)
tag_associations = OneToMany('TagAssociation')
items = association_proxy(
'tag_associations', 'item',
creator=lambda item: TagAssociation(item=item)
)
class TagAssociation(Entity):
tagged_by = Field(Unicode(255))
tag = ManyToOne('Tag')
item = ManyToOne('Item')
setup_all()
metadata.bind = 'sqlite:///'
metadata.create_all()
car = Item(title=u'Car')
bike = Item(title=u'Bike')
apple = Item(title=u'Apple')
orange = Item(title=u'Orange')
transport = Tag(name=u'Transport')
fruit = Tag(name=u'Fruit')
color = Tag(name=u'Color')
car.tags.append(transport)
bike.tags.append(transport)
apple.tags.append(fruit)
orange.tags.append(fruit)
orange.tags.append(color)
session.commit()
# get the car and print its tags
car = Item.get(1)
print car.title
print [tag.name for tag in car.tags]
print
# get the fruit tag and print its tagged items
fruit = Tag.get(2)
print fruit.name
print [item.title for item in fruit.items]
print
# count the number of items tagged as transport
print TagAssociation.query.filter(
TagAssociation.tag.has(name=u'Transport')
).count()
I hope this helps.
--
Jonathan LaCour
http://cleverdevil.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---