On Wed, 2007-12-09 at 16:15 -0700, badfrog wrote:
> OK... I almost found what I need for the first question with __dict__,
> but this gives me a bunch values that I don't care about, and I'd have
> to remember to append "_SO_val_" to attribute names to get values that
> I do care about. Quick example:
>
> class foo(SQLObject):
> name = StringCol(length=50)
>
> foo(name="Foo1")
>
> foo.get(1).__dict__
> { ..., '_SO_val_name': 'Foo1', ..., 'id': 1, ... }
>
> So... and I know this might be SQLObject-specific, but... is there any
> built-in way to get a dict that would *only* contain {'id' : 1,
> 'name':'Foo1'} in this example?
>
> I also found a decent-enough example that shows how to set widget
> values, thereby answering Q2.here:
> http://docs.turbogears.org/1.0/RoughDocs/ModifyWidgetDefault
Hi BadFrog, you can indeed pass in a regular SO or SA object. The
widgets will look at whatever you pass in to the 'value' paramater
( which is also the first unnamed paramater ) and use any attributes of
that object, be it dictionary or class. A common idiom is to pass in a
dictionary of defaults at instantiation time and then clobber it with a
a real object at rendering time if your are editing an existing item
instead of making a new one. If the widget has a field with the same
name as the attribute, the form field will get filled in automatically.
Sometimes you get a sort of 'impedance mismatch'. For example, you have
a select field with integer indexes and what you want is something
named, ( or vice versa ) perhaps it's a relation on the model object. An
nice way to work around that is to put a property in your model to do
the conversion. So for instance if you have a model called Post and it
has a relation called 'tags', but you want your post editing form to
have a select field with the right tags already selected with the
corresponding integer of the tag's id, you can add a property to your
Post model that does the conversion for you. Then give the widget field
the name of the property. In this case, it might be a property called
'id_tags' that returns a list of the related tag objects ids.
Here is a code snippet of how I do it, though I use SA. You can see that
I also pack a bunch of extra features up in my model too, which allows
me to use one generic crud controller class for about 90% of the
resources on the site. I pass a reference to the model *class* into the
crud controller and that way it can get whatever fields are necessary to
instantiate widgets by call ModelClass._edit_fields etc
####################################################################################################
# RESOURCE Event
event_table = Table('event', metadata,
Column( 'id', Integer, primary_key=True ),
Column( 'topic', Unicode(255) ),
Column( 'date', Date ),
Column( 'start_time', Time ),
Column( 'end_time', Time ),
Column( 'location', Unicode(255) ),
Column( 'cost', Float(4) ),
Column( 'cost_child', Float(4) ),
Column( 'teaser', Unicode ),
Column( 'description', Unicode )
)
class Event( object ):
def get_speaker_options():
return [ (speaker.id, speaker) for speaker in
Speaker.select() ]
# property to change list speaker ids to Speaker objects
def get_id_speakers( self ):
return [ speaker.id for speaker in self.speakers ]
def set_id_speakers(self, id_list):
self.speakers = [ Speaker.get(id) for id in id_list ]
id_speakers = property( get_id_speakers, set_id_speakers )
_name = "event"
_list_fields = [ ('Topic', 'topic'), ('Speaker(s)', 'speakers' ),
('Date', 'date'), ('Start Time', 'start_time'),
('End Time', 'end_time'), ('Cost', 'cost')]
_edit_fields = [
w.TextField("topic", label="Topic: ",
validator=v.UnicodeString() ),
w.TextField("date", label="Date: ", default=datetime.today(),
validator=v.DateConverter() ),
w.TextField("start_time", label="Start Time: ",
default=datetime.now(), validator=v.TimeConverter(use_datetime=True) ),
w.TextField("end_time", label="End Time: ",
default=datetime.now(), validator=v.TimeConverter(use_datetime=True) ),
w.TextField("location", label="Location: ",
validator=v.UnicodeString() ),
w.TextField("cost", label="Cost: ",
validator=v.UnicodeString() ),
w.TextField("cost_child", label="Cost per child: ",
validator=v.UnicodeString() ),
w.TextArea("teaser", label="Teaser: ",
validator=v.UnicodeString() ),
w.TextArea("description", label="Description: ",
validator=v.UnicodeString() ),
w.MultipleSelectField("id_speakers", label="Speakers: ",
options=get_speaker_options, validator=v.NotEmpty() )
]
def __str__(self):
return str(self.topic)
# assign_mappers for all our resources
assign( Event, event_table,
properties={
'speakers': relation( Speaker,
secondary=event_speaker_table, lazy=False ),
'registrations': relation( Registration )
})
>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---