Re: [SQLObject] Checking for modification of entry
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ryan K. <rya...@co...> - 2005-03-04 13:59:32
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> I don't know if this is that fastest or cleanest way to do what Ian suggested, but here is what I came up with:<br> <br> import sqlobject<br> <br> class rwksqlobj(sqlobject.SQLObject):<br> def as_dict(self):<br> mycols=self._columns<br> mydict={}<br> for curcol in mycols:<br> mydict[curcol._name]=self.__getattribute__(curcol._name)<br> return mydict<br> <br> def set_dict(self,dictin):<br> setstr='';<br> for key,item in dictin.iteritems():<br> if len(setstr)>0:<br> setstr=setstr+', '<br> setstr=setstr+key+'=\''+item+'\''<br> exec('self.set('+setstr+')')<br> <br> It seems to work, but I am open to suggestions.<br> <br> Ryan<br> <br> Martin Clausen wrote: <blockquote cite="mid...@ma..." type="cite"> <pre wrap="">I guess: <a class="moz-txt-link-freetext" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389916</a> would be useful to achieve Ian's first bullit. Would it have to be adapted to be used with an SQLObject instance ? On Thu, 03 Mar 2005 15:35:28 -0600, Ian Bicking <a class="moz-txt-link-rfc2396E" href="mailto:ia...@co..."><ia...@co...></a> wrote: </pre> <blockquote type="cite"> <pre wrap="">Ryan Krauss wrote: </pre> <blockquote type="cite"> <pre wrap="">I am new to sqlobject and it seems to be one of the best object-relational python mappers out there. I have been searching the archives for an answer to my question and have seen some similar discussions but now found any complete and simple answers. What I would like to do is use sqlobject and wxPython to allow a user to view and edit information in the database. I would like a fairly straight forward way to test if the information has been modified by the user and then update the database if it has. It seems like creating a temporary object based on what is currently in the wxPython display and then comparing that to the object from the database would accomplish this, but I don't know how to create a temporary object (though I have seen much discussion in the mailing list). Can anyone think of a better way to do this or tell me how to create temporary object? </pre> </blockquote> <pre wrap="">I'd do this like so: * Create a method or function to turn a SQLObject instance into a dictionary of values. Also a corresponding method to assign values from such a dictionary (db_instance.set(**a_dict) is the obvious way). * Use that dictionary to populate the wxPython form. * Create a method or function that takes the form and turns it into a dictionary of values. Then you can do: dict_values = db_instance.as_dict() form_result = fill_form(defaults=dict_values) if form_result != db_instance.as_dict(): db_instance.set(**form_result) -- Ian Bicking / <a class="moz-txt-link-abbreviated" href="mailto:ia...@co...">ia...@co...</a> / <a class="moz-txt-link-freetext" href="http://blog.ianbicking.org">http://blog.ianbicking.org</a> ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. <a class="moz-txt-link-freetext" href="http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click">http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click</a> _______________________________________________ sqlobject-discuss mailing list <a class="moz-txt-link-abbreviated" href="mailto:sql...@li...">sql...@li...</a> <a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss">https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss</a> </pre> </blockquote> <pre wrap=""><!----> ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. <a class="moz-txt-link-freetext" href="http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click">http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click</a> _______________________________________________ sqlobject-discuss mailing list <a class="moz-txt-link-abbreviated" href="mailto:sql...@li...">sql...@li...</a> <a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss">https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss</a> </pre> </blockquote> <br> </body> </html> |