I'm writing a contact management system that gathers data from various
canonical sources, collates it, then sends it to various MailChimp
accounts so that their owners can send emails to our contact lists
using MailChimp's services. I need to keep track of contacts who've
unsubscribed from these emails.
So, my app needs to keep track of which contacts have been synced to
the various MailChimp accounts' lists, and it does so through an
association object between the Contact table and the Account table.
It's not simply a many-to-many table because I wanted to store the
"unsubscribed" flag inside the relationship.
So, I've now got code like this:
class Account(Base):
__tablename__ = 'account'
account_name = Column(String(128), primary_key=True)
api_key = Column(String(128), nullable=False)
owner_name = Column(String(256), nullable=False)
synced_contacts = association_proxy('synced_contact_assocs',
'contact')
class Contact(Base):
__tablename__ = 'contact'
id = Column(Integer, primary_key=True)
email = Column(String(128), nullable=False)
first_name = Column(String(64), nullable=False)
last_name = Column(String(64), nullable=False)
bounced = Column(Boolean, default=False)
class AccountSyncedContact(Base, ZKTableAudit, MySQLTable):
__tablename__ = 'account_synced_contact'
account_name = Column(String(128),
ForeignKey('account.account_name'), primary_key=True)
contact_id = Column(Integer, ForeignKey('contact.id'),
primary_key=True)
unsubscribed = Column(Boolean, nullable=False, default=False)
account = relationship('Account',
backref=backref('synced_contact_assocs', cascade='all, delete-
orphan'))
contact = relationship('Contact')
I access the list of contacts which have been synced to a certain
account by using Account.synced_contacts. However, I now realize that
I don't know how to actually read and write
AccountSyncedContact.unsubscribed from the Account.synced_contacts
list. I haven't found any documentation on it.
So, I'm primarily wondering how to get at that unsubscribed flag, both
for reading and writing, after the AccountSyncedContact row has
already been written.
Contacts are known by multiple Accounts, but unsubscription is Account-
specific, so I can't just add the unsubscribed flag to Contact itself.
It'd be really cool if I could access it from a Contact object that
I've acquired through an Account's synced_contacts list, though.
If that's not possible, would it make more sense to just dump the
entire Association Object method, and just use two many-to-many
relationships: one for "this contact is synced to this account" and
the other for "this contact is unsubscribed from this account"?
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en.