Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:14:30+00:00 2026-06-06T18:14:30+00:00

In my model, I have a base object class A which holds a set

  • 0

In my model, I have a base object class A which holds a set of attributes. Each object of A can be connected to any other object of A through a many-to-many association object Context. This Context class holds a key for every connection.

class A(Base):
    __tablename__ = "a"
    id = Column(Integer, primary_key=True)

    # other attributes
    value = Column(Integer)

    def __init__(self, value):
        self.value = value

class Context(Base):
    __tablename__ = "context"
    holder_id = Column(Integer, ForeignKey("a.id"), nullable=False, primary_key=True)
    attachment_id = Column(Integer, ForeignKey("a.id"), nullable=False, primary_key=True)
    key = Column(String, primary_key=True)

    holder = relationship(A,
        primaryjoin=lambda: Context.holder_id==A.id)

    attachment = relationship(A,
        primaryjoin=lambda: Context.attachment_id==A.id)

The Context class therefore stores 3-tuples of the form ‘Holder object a1 holds attachment a2 with key k1’.

I now want to have a smart proxy collection on A which groups this relationship on the Context.key such that I can use it as follows:

a1 = A(1)
a1.context["key_1"] = set([A(2)])
a1.context["key_2"] = set([A(3), A(4), A(5)])

a1.context["key_1"].add(A(10))

a100 = A(100)
a100.context = {
  "key_1": set([A(101)])
}

How do I have to define context?

I know there is an example for modelling a dict–set proxy in the SQLAlchemy documentation but somehow I am not able to get it to work in a self-referential situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:14:32+00:00Added an answer on June 6, 2026 at 6:14 pm

    Unless I’m blanking a bit (which is possible…gin…) this can’t be done with association proxy directly on the relationship because a1.context would need to be a collection where each element has a unique key, then the collection can be a dictionary – but there is no such collection here, as a1 can have many Context objects with the same key. Assoc prox’s simple way of reducing a collection of objects to a collection of an attribute on each member object doesn’t apply to this.

    So if you really want this, and your structure can’t change, just do what association proxy does, just in a hardcoded way, which is, build a proxying collection ! actually two, I think. Not too big a deal, just need to turn the crank….quite a bit, make sure you add tests for every manipulation here:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.ext.associationproxy import association_proxy
    import itertools
    
    Base= declarative_base()
    
    class A(Base):
        __tablename__ = "a"
        id = Column(Integer, primary_key=True)
    
        # other attributes
        value = Column(Integer)
    
        def __init__(self, value):
            self.value = value
    
        @property
        def context(self):
            return HolderBySetDict(self)
    
        @context.setter
        def context(self, dict_):
            toremove = set([ctx for ctx in self.attached_by if ctx.key not in dict_])
            toadd = set([Context(key=k, holder=item) for k, v in dict_.items()
                                for item in itertools.chain(v)])
            self.attached_by.update(toadd)
            self.attached_by.difference_update(toremove)
    
    class HolderBySetDict(object):
        def __init__(self, parent):
            self.parent = parent
    
        def __iter__(self):
            return iter(self.keys())
    
        def keys(self):
            return list(set(ctx.key for ctx in self.parent.attached_by))
    
        def __delitem__(self, key):
            toremove = set([ctx for ctx in self.parent.attached_by if ctx.key == key])
            self.parent.attached_by.difference_update(toremove)
    
        def __getitem__(self, key):
            return HolderBySet(self.parent, key)
    
        def __setitem__(self, key, value):
            current = set([ctx for ctx in self.parent.attached_by if ctx.key == key])
            toremove = set([ctx for ctx in current if ctx.holder not in value])
            toadd = set([Context(key=key,holder=v) for v in value if v not in current])
            self.parent.attached_by.update(toadd)
            self.parent.attached_by.difference_update(toremove)
    
        # exercises !  for the reader !
        #def __contains__(self, key):
        #def values(self):
        #def items(self):
        # ....
    
    
    class HolderBySet(object):
        def __init__(self, parent, key):
            self.key = key
            self.parent = parent
    
        def __iter__(self):
            return iter([ctx.holder for ctx
                        in self.parent.attached_by if ctx.key == self.key])
    
        def update(self, items):
            curr = set([ctx.holder for ctx
                                in self.parent.attached_by if ctx.key==self.key])
            toadd = set(items).difference(curr)
            self.parent.attached_by.update(
                    [Context(key=self.key, holder=item) for item in toadd])
    
        def remove(self, item):
            for ctx in self.parent.attached_by:
                if ctx.key == self.key and ctx.holder is item:
                    self.parent.attached_by.remove(ctx)
                    break
            else:
                raise ValueError("Value not present")
    
        def add(self, item):
            for ctx in self.parent.attached_by:
                if ctx.key == self.key and ctx.holder is item:
                    break
            else:
                self.parent.attached_by.add(Context(key=self.key, holder=item))
    
        # more exercises !  for the reader !
        #def __contains__(self, key):
        #def union(self):
        #def intersection(self):
        #def difference(self):
        #def difference_update(self):
        # ....
    
    class Context(Base):
        __tablename__ = "context"
        holder_id = Column(Integer, ForeignKey("a.id"), nullable=False, primary_key=True)
        attachment_id = Column(Integer, ForeignKey("a.id"), nullable=False, primary_key=True)
        key = Column(String, primary_key=True)
    
        holder = relationship(A,
            primaryjoin=lambda: Context.holder_id==A.id)
    
        attachment = relationship(A,
            primaryjoin=lambda: Context.attachment_id==A.id,
            backref=backref("attached_by", collection_class=set))
    
    a1 = A(1)
    a2 = A(2)
    a3, a4, a5 = A(3), A(4), A(5)
    a1.context["key_1"] = set([a2])
    a1.context["key_2"] = set([a3, a4, a5])
    
    assert set([ctx.holder for ctx in a1.attached_by if ctx.key == "key_1"]) == set([a2])
    assert set([ctx.holder for ctx in a1.attached_by if ctx.key == "key_2"]) == set([a3, a4, a5])
    
    a10 = A(10)
    a1.context["key_1"].add(a10)
    print set([ctx.holder for ctx in a1.attached_by if ctx.key == "key_1"])
    assert set([ctx.holder for ctx in a1.attached_by if ctx.key == "key_1"]) == set([a2, a10])
    
    a100 = A(100)
    a101 = A(101)
    a100.context = {
      "key_1": set([a101])
    }
    assert set([ctx.holder for ctx in a100.attached_by]) == set([a101])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my zf application i have base model class Application_Model, which connects to db
I have a model in my rails application which is class Person < ActiveRecord::Base
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have a model called Category which looks like this: class Category < ActiveRecord::Base
My situation I have an entity model with a base class ItemBase which defines
In my model I have: class Log < ActiveRecord::Base serialize :data ... def self.recover(table_name,
I have a model defined this way class Lga < ActiveRecord::Base validates_uniqueness_of :code validates_presence_of
I have a model: review.rb class Review < ActiveRecord::Base validates :title, :presence => true
I have a model, for example : class Account < ActiveRecord::Base before_create :build_dependencies def
I have a model, Show and a module Utilities class Show < ActiveRecord::Base include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.