On 10/18/06, Lee McFadden <[EMAIL PROTECTED]> wrote:
>
> On 10/18/06, Tor Hildrum <[EMAIL PROTECTED]> wrote:
> >
> >
> > I've tried passing in a dict to my 'model constructor':
> > -----------------------
> > newBook = Book(kw)
> > ----------------------
> >
>
> For this to work you'd need to use:
>
> newBook = Book(**kw)
>
> However, you'd have to be certain that there's nothing in the dict's
> keys other than in the Book object's column names otherwise SO may
> complain (not 100% sure on that as I've never looked into how SO
> initialises a new object).

Perfect.

> The *preferred* way of creating a new object is to pass each column
> and it's data as a keyword/value pair (I'm going to make up the column
> names for your Book class, but you should get the idea):
>
> new_book = Book(title=kw['title'], author=kw['author'],
> isbn=kw['isbn'], publisher=kw['publisher'])

That's not pretty when the table has a lot of attributes, it also
lends itself to problems when/if the db-schema is updated.

I'd rather just use a dict and check it against my db-schema.

One more thing, since I can't access the SQLObject documentation.
**kw holds all the fields in my form, some of them will almost always be empty.
(there are a lot of information that we won't have for most of the books)
Say I have a field in my database:
--------------------------
Num_revision =   IntCol()
-------------------------
Which is the revision number of the book, this will seldom have a value.
In kw I get:
kw['Num_revision'] == '' # an empty string

Since Num_revision is an IntCol() I have to cast it. So while checking kw
I would do something like:
-----------------------------
if( schema[key][type] == 'int' ):
   kw[key] = int( kw[key] )
-----------------------------
key is the attribute, in this case Num_revision.

Now, if Num_revision is '', this will error out.
>>> Num_revisions = int( '' )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int():

So, what I would like to do first:
---------------------------------
for key in kw.keys():
  if( kw[key] == ''):
    del kw[key]
--------------------------------

This means I only check and add the values the user supplies. I don't
need the rest, they can stay blank.

However, SQLObject refuses me to do this.
File 
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/SQLObject-0.7.1-py2.4.egg/sqlobject/main.py",
line 1217, in _create
    raise TypeError, "%s() did not get expected keyword argument %s" %
(self.__class__.__name__, column.name)
TypeError: Book() did not get expected keyword argument Num_revisions

Is there any way I can get SQLObject to only require me to pass it the
key(ISBN), and have all other fields be optional?

Like this(postgres):
-------------------------------
library=> INSERT INTO book (isb_n) VALUES('1234567');
INSERT 0 1
library=> SELECT * FROM book;
 id |  isb_n  | title | series....
----+---------+-------+--------+....
  1 | 1234567 |       |        |....
(1 row)
----------------------------------
Hope this makes sense :)

regards

Tor

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to