The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Pylons and FormAlchemy can eat a dick - SOLVED

Jimmy KingJimmy King Registered User regular
edited November 2010 in Help / Advice Forum
I posted this in the programming thread and it's not getting any love yet, although server logs show people have looked at it and checked out my links. I don't have any time for fucking around on this, so I'm posting here, too just in case someone here knows what the deal is. As far as I can tell this should "just work", only it doesn't and I can't find any code examples or tutorials covering this that are full, working, fully explained examples. They are all just snippets that assume I know stuff that I quite clearly don't.

I would punch Pylons right in the dick if it had one to punch. I'm pretty new to Python and brand new to Pylons and I can't get something that seems to be dead fucking simple to work. If anyone can explain to me wtf I'm missing, that would be great.

I've got a super, super simple test object setup to try to get one to many objects working with FormAlchemy. I'm just using the built in admin interface right now. It shows both of my object types, but the sub object does not have any selector for which parent object it is related to.

I'll leave my admin interface up at http://65.110.60.90:5000/admin so that you can see it.

This page shows what SqlAlchemy says I should be seeing, more or less. See how their's shows related classes and for specific objects, has the relationship there? This UI also looks nicer than mine even though I've got the news versions of Pylons, SqlAlchemy, and FormAlchemy. http://docs.formalchemy.org/ext/pylons.html#administration-interface

model/__init__.py
"""The application's model objects"""
from test.model.meta import Session, Base
import sqlalchemy as sa
from sqlalchemy import types, schema

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    Session.configure(bind=engine)

class ParentObject(Base):
    __tablename__ = "parent_object"

    id = sa.Column(types.Integer, primary_key=True)
    name = sa.Column(types.Unicode(255))

class SubObject(Base):
    __tablename__ = "sub_object"

    id = sa.Column(types.Integer, primary_key=True)
    name = sa.Column(types.Unicode(255))
    parent_id = sa.Column(types.Integer, sa.ForeignKey(ParentObject.id))

controllers/admin.py
import logging
from formalchemy.ext.pylons.controller import ModelsController
from webhelpers.paginate import Page
from test.lib.base import BaseController, render
from test import model
from test import forms
from test.model import meta

log = logging.getLogger(__name__)

class AdminControllerBase(BaseController):
    model = model # where your SQLAlchemy mappers are
    forms = forms # module containing FormAlchemy fieldsets definitions
    def Session(self): # Session factory
        return meta.Session

    ## customize the query for a model listing
    # def get_page(self):
    #     if self.model_name == 'Foo':
    #         return Page(meta.Session.query(model.Foo).order_by(model.Foo.bar)
    #     return super(AdminControllerBase, self).get_page()

AdminController = ModelsController(AdminControllerBase,
                                   prefix_name='admin',
                                   member_name='model',
                                   collection_name='models',
                                  )

Jimmy King on

Posts

  • Jimmy KingJimmy King Registered User regular
    edited November 2010
    Solved. The answer was buried on sqlalchemy.org and just some lucky googling pulled it up for me. The original tutorial left out some critical details about how to set up the models.

    In case anyone else ever runs across this, here's the deal. On top of defining the ForeignKey on the class attributes, you also have to specifically set up the relationship on that foreign key with sqlalchemy.orm.relationship.

    My model/__init__.py now looks like this:
    """The application's model objects"""
    from test.model.meta import Session, Base
    import sqlalchemy as sa
    from sqlalchemy import types, schema
    from sqlalchemy.orm import relationship, backref
    
    def init_model(engine):
        """Call me before using any of the tables or classes in the model"""
        Session.configure(bind=engine)
    
    class ParentObject(Base):
        __tablename__ = "parent_object"
    
        id = sa.Column(types.Integer, primary_key=True)
        name = sa.Column(types.Unicode(255))
    
        def __unicode__(self):
            return self.name
    
    class SubObject(Base):
        __tablename__ = "sub_object"
    
        id = sa.Column(types.Integer, primary_key=True)
        name = sa.Column(types.Unicode(255))
        parent_id = sa.Column(types.Integer, sa.ForeignKey('parent_object.id'))
    
        parent = relationship(ParentObject, backref=backref("sub_objects"))
        def __unicode__(self):
            return self.name
    
    
    [

    Jimmy King on
Sign In or Register to comment.