One to Many

42 views
Skip to first unread message

Adrian Klaver

unread,
Feb 28, 2013, 10:34:09 AM2/28/13
to project...@googlegroups.com
I have set up a relationship between two tables as shown below:

class Plant(Base):

__table__= Table("plant1", Base.metadata,
autoload=True, autoload_with=engine)


proj = relationship("Projection")
def __unicode__(self):
return self.common

class Admin(EntityAdmin):
list_filter = ["plant_type"]
list_search = ["common"]
form_size = (400,200)
list_display = ["common", "genus", "species", "plant_type",
"season"]
form_display = ["common", "genus", "species", "plant_type",
"season",
"proj"]
form_size = (500, 300)


class Projection(Base):

__table__ = Table("projection", Base.metadata,
autoload=True, autoload_with=engine)

plant = relationship("Plant")

class Admin(EntityAdmin):
form_size = (400,200)
field_attributes = {"qty": {"precision": 2}}
list_display = ["year", "qty", "plant"]



I get a form view where I have many projections for each plant, which is
what I want. What I am trying to figure out is how to have the
p_item_no(FK) filled in automatically from the plant when I create a new
record in the form?

On a somewhat related note I get the following error when I run the form:

ERROR:camelot.admin.validator.object_validator:programming error while
validating object
Traceback (most recent call last):
File
"/usr/local/lib/python2.7/site-packages/Camelot-12.06.29-py2.7.egg/camelot/admin/validator/object_validator.py",
line 165, in isValid
messages = self.objectValidity(entity_instance)
File
"/usr/local/lib/python2.7/site-packages/Camelot-12.06.29-py2.7.egg/camelot/admin/validator/object_validator.py",
line 106, in objectValidity
return self.validate_object( entity_instance )
File
"/usr/local/lib/python2.7/site-packages/Camelot-12.06.29-py2.7.egg/camelot/admin/validator/entity_validator.py",
line 45, in validate_object
return super( EntityValidator, self ).validate_object( obj )
File
"/usr/local/lib/python2.7/site-packages/Camelot-12.06.29-py2.7.egg/camelot/admin/validator/object_validator.py",
line 135, in validate_object
elif (attributes['delegate'] == delegates.Many2OneDelegate) and
(not value.id):
AttributeError: 'Plant' object has no attribute 'id'


My guess is occurring because I am using a reflected table and it does
not have an id column. It does have p_item_no which is the PK. Is there
a way to either turn of that particular validation or tell the framework
the actual PK?

Thanks,
--
Adrian Klaver
adr...@aklaver.org

Adrian Klaver

unread,
Feb 28, 2013, 5:27:05 PM2/28/13
to project...@googlegroups.com
Originally sent this email from wrong account, trying again.
the actual PK? In between sending of messages I found the TODO in the
code about changing to look for PK instead of id.


--
Adrian Klaver
adrian...@gmail.com

Erik Janssens

unread,
Mar 1, 2013, 4:16:24 AM3/1/13
to project...@googlegroups.com
1) this is because the releationship is setup in both directions. in plain
SQLA, the relationship is to be set in one direction, and the 'backref'
argument is used to define the other side, so

proj = relationship('Projection', backref='plant')

this is unlike Elixir/Camelot Relationship properties that detect this
and 'connect' both Relationships

2) that's indeed a bug in the ObjectValidator, fixed in the master
branch on gitorious

Adrian Klaver

unread,
Mar 1, 2013, 10:23:27 AM3/1/13
to project...@googlegroups.com, Erik Janssens
On 03/01/2013 01:16 AM, Erik Janssens wrote:
> 1) this is because the releationship is setup in both directions. in plain
> SQLA, the relationship is to be set in one direction, and the 'backref'
> argument is used to define the other side, so
>
> proj = relationship('Projection', backref='plant')
>
> this is unlike Elixir/Camelot Relationship properties that detect this
> and 'connect' both Relationships

To get it to work I had to change the above, see code below.

>
> 2) that's indeed a bug in the ObjectValidator, fixed in the master
> branch on gitorious

Got it, thanks.
>


class Plant(Base):

__table__= Table("plant1", Base.metadata,
autoload=True, autoload_with=engine)

proj = relationship("Projection", back_populates="plant")
def __unicode__(self):
return self.common

class Admin(EntityAdmin):
list_filter = ["plant_type"]
list_search = ["common"]
form_size = (400,200)
list_display = ["common", "genus", "species", "plant_type",
"season"]
form_display = ["common", "genus", "species", "plant_type",
"season",
"proj"]
form_size = (500, 300)


class Projection(Base):

__table__ = Table("projection", Base.metadata,
autoload=True, autoload_with=engine)


plant = relationship("Plant", back_populates="proj")

class Admin(EntityAdmin):
form_size = (400,200)
#field_attributes = {"qty": {"precision": 2}}
list_display = ["year", "qty", "plant"]

--
Adrian Klaver
adrian...@gmail.com

Erik Janssens

unread,
Mar 3, 2013, 9:11:50 AM3/3/13
to project...@googlegroups.com
good to know that works as well ;)

Adrian Klaver

unread,
Mar 3, 2013, 3:15:25 PM3/3/13
to project...@googlegroups.com, Erik Janssens
On 03/03/2013 06:11 AM, Erik Janssens wrote:
> good to know that works as well ;)

To follow up. When I tried your original suggestion I got error messages
to to the effect that the relationship could not be established. I did
some digging around in the SQLA docs and came up with the back_populates
work around. I then moved on to a more complicated layout involving more
FK relationships. Even using the back_populates method I could not get
the relationships established. After much frustration I found out that
it came down to schema qualification. I had schema qualified one of the
tables, but not the others. When I fixed that(schema qualified all the
tables) the back_populates method worked again. Your message above
prompted me to see if your original suggestion would work with my
changes, it did. Still not quite sure why, the Postgres database
search_path is set to cover all the schemas and the table names are
unique across the schemas. A puzzle for another day:) In any event,
lesson learned, be consistent in naming!


--
Adrian Klaver
adrian...@gmail.com
Reply all
Reply to author
Forward
0 new messages