Django Using Stored Procedure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#Model
from django.db import models
from django.db import connection
class MY_UTIL():
'''
Its pretty clean and nice way to define stored procedures inside the class, especially models.py (I hope someone will write a automatic generation for all stored procedure calls soon.)
'''
    def log_message(self, control_in, message_in):
        cursor = connection.cursor()
        ret = cursor.callproc("MY_UTIL.LOG_MESSAGE", (control_in, message_in))# calls PROCEDURE named LOG_MESSAGE which resides in MY_UTIL Package
        cursor.close()
        return ret

#View
from django import newform as form # here we are using newforms, please note its not available NOW in django standard package.

class MessageForm(forms.Form):
    email = forms.EmailField(max_length=100, help_text="Please enter your email")
    subject = forms.CharField(max_length=50,help_text="Please provide a short desc")

def message(request):
    if request.method == 'POST':
        form = MessageForm(request.POST)
    else:
        form = MessageForm()
    if form.is_valid():
        my_util = MY_UTIL()
        ret = my_util.log_message(request.POST['email'], request.POST['subject'])
        return HttpResponseRedirect('/done/')
    return render_to_response('form.html', {'form':form})

More like this

  1. Function/Stored Procedure Manager by axiak 5 years, 11 months ago
  2. Binding pre-existing tables with dynamically created class models by rodsenra 5 years, 10 months ago
  3. OracleAuthBackend by nosrednakram 3 years, 9 months ago
  4. Persistent connection to PostgreSQL database by mike_tk 3 years, 8 months ago
  5. Dynamic Models Revisited by Ben 5 years, 7 months ago

Comments

(Forgotten your password?)