Hello,
i want to build a Django Application which is using stored procedures for saving and deleting the data in the database and using views for reading the data. At the moment i override the save and delete method in the model class like this:
def save(self, *args, **kwargs):
with connection.cursor() as cursor:
print(connection['DATABASES'])
if self.mdt_id:
query = "SELECT * FROM fnc_fdb_mandator_update(ROW(%s,%s,%s,%s,%s,%s,%s))"
else:
query = "SELECT * FROM fnc_fdb_mandator_insert(ROW(%s,%s,%s,%s,%s,%s,%s))"
cursor.execute(query,
[self.mdt_id,
self.mdt_guid,
self.mdt_name,
self.mdt_insert_date,
self.mdt_insert_user,
self.mdt_update_date,
self.mdt_update_user])
row = cursor.fetchone()
self.mdt_id = row[0]
self.mdt_guid = row[1]
self.mdt_name = row[2]
self.mdt_insert_date = row[3]
self.mdt_insert_user = row[4]
self.mdt_update_date = row[5]
return True
def delete(self, *args, **kwargs):
with connection.cursor() as cursor:
query = "SELECT * FROM fnc_fdb_mandator_delete(%s)"
cursor.execute(query,
[self.mdt_id])
row = cursor.fetchone()
return row[0]
This approach works. But i read a little bit more and find out that the save method makes much more, like pre and post events. And after i had a look into the source code from Django, i’am not sure that my approach is good. I saw that the Django model using a manager and maybe it would be better to write an own manager?!
Has anybody experience with this? What would be the best approach for this problem?