Hi,
If I have an ORM object, it is sometimes convenient to be able to infer
the class directly. Eg. consider this function.
def add_patient_obj(session, patient_obj):
""" Check if object primary key exists in db. If so,exit, else
add."""
pid = patient_obj.id
#print session.query(Patient).filter_by(id=pid).count()
if session.query(Patient).filter_by(id=pid).count() > 0:
print "Patient object with id %s is already in db."%pid
exit
else:
session.save(patient_obj)
session.commit()
But I want a generic version. Since patient_obj knows what class is
belongs to, it should be possible not to have to state the class directly,
which here is Patient.
I have done the following, which works, but is hideous, horrible, ugly,
fragile hack. Can anyone suggest a better way of doing this?
Please CC me on any reply. Thanks in advance.
Regards, Faheem.
def add_obj(session, obj):
""" Check if object primary key exists in db. If so,exit, else
add.
"""
c = str(type(obj)).split("'")[1].split(".")[1]
s = "q = session.query("+ c +")"
exec(s)
pid = obj.id
if q.filter_by(id=pid).count() > 0:
print "Patient object with id %s is already in db."%pid
exit
else:
session.save(obj)
session.commit()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---