I have a four classes like so: Group, Parent, Child, Toy.
Grouphas aparentsrelationship pointing toParentParenthas achildrenrelationship pointing toChildChildhas atoysrelationship pointing toToy
Parent has a toys association_proxy that produces all the Toys that the Parent's children have.
I want to be able to get all the Toys in a Group. I tried to create an association_proxy on Group that links to Parent's toys, but it produces this:
[[<Toy 1>, <Toy 2>], [], [], []]
when I want this:
[<Toy 1>, <Toy 2>]
If the Parent's children don't have any Toys, then the toys association proxy is []. However, the second association proxy doesn't know to exclude the empty lists. Also, the lists should be collapsed. Is there anyway to get this to work?
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
name = db.Column(db.String(80, convert_unicode=True))
# relationships
parents = db.relationship('Parent', backref='group')
toys = association_proxy('parents', 'toys')
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id', ondelete='CASCADE'))
created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
first_name = db.Column(db.String(80, convert_unicode=True))
last_name = db.Column(db.String(80, convert_unicode=True))
children = db.relationship('Child', backref='parent', cascade='all, delete')
toys = association_proxy('children', 'toys')
class Child(db.Model):
id = db.Column(db.Integer, primary_key=True)
parent_id = db.Column(db.Integer, db.ForeignKey('parent.id', ondelete='CASCADE'))
created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
class Toy(db.Model):
id = db.Column(db.Integer, primary_key=True)
child_id = db.Column(db.Integer, db.ForeignKey('child.id', ondelete='CASCADE'))
created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
child = db.relationship('Child', backref=db.backref("toys",cascade="all, delete-orphan", order_by="desc(Toy.id)"))