Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 193365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:30:15+00:00 2026-05-11T16:30:15+00:00

I am designing a fairly complex database, and know that some of my queries

  • 0

I am designing a fairly complex database, and know that some of my queries will be far outside the scope of Django’s ORM. Has anyone integrated SP’s with Django’s ORM successfully? If so, what RDBMS and how did you do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:30:16+00:00Added an answer on May 11, 2026 at 4:30 pm

    We (musicpictures.com / eviscape.com) wrote that django snippet but its not the whole story (actually that code was only tested on Oracle at that time).

    Stored procedures make sense when you want to reuse tried and tested SP code or where one SP call will be faster than multiple calls to the database – or where security requires moderated access to the database – or where the queries are very complicated / multistep. We’re using a hybrid model/SP approach against both Oracle and Postgres databases.

    The trick is to make it easy to use and keep it “django” like. We use a make_instance function which takes the result of cursor and creates instances of a model populated from the cursor. This is nice because the cursor might return additional fields. Then you can use those instances in your code / templates much like normal django model objects.

    def make_instance(instance, values):
        '''
        Copied from eviscape.com
    
        generates an instance for dict data coming from an sp
    
        expects:
            instance - empty instance of the model to generate
            values -   dictionary from a stored procedure with keys that are named like the
                       model's attributes
        use like:
            evis = InstanceGenerator(Evis(), evis_dict_from_SP)
    
        >>> make_instance(Evis(), {'evi_id': '007', 'evi_subject': 'J. Bond, Architect'})
        <Evis: J. Bond, Architect>
    
        '''
        attributes = filter(lambda x: not x.startswith('_'), instance.__dict__.keys())
    
        for a in attributes:
            try:
                # field names from oracle sp are UPPER CASE
                # we want to put PIC_ID in pic_id etc.
                setattr(instance, a, values[a.upper()])
                del values[a.upper()]
            except:
                pass
    
        #add any values that are not in the model as well
        for v in values.keys():
            setattr(instance, v, values[v])
            #print 'setting %s to %s' % (v, values[v])
    
        return instance
    

    # Use it like this:

    pictures = [make_instance(Pictures(), item) for item in picture_dict]
    

    # And here are some helper functions:

    def call_an_sp(self, var):
        cursor = connection.cursor()
        cursor.callproc("fn_sp_name", (var,))
        return self.fn_generic(cursor)
    
    
    def fn_generic(self, cursor):
        msg = cursor.fetchone()[0]
        cursor.execute('FETCH ALL IN "%s"' % msg)
        thing = create_dict_from_cursor(cursor)
        cursor.close()
        return thing
    
    def create_dict_from_cursor(cursor):
        rows = cursor.fetchall()
        # DEBUG settings (used to) affect what gets returned. 
        if DEBUG:
            desc = [item[0] for item in cursor.cursor.description]
        else:
            desc = [item[0] for item in cursor.description]
        return [dict(zip(desc, item)) for item in rows]    
    

    cheers, Simon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a relative VBA novice, and designing a fairly complex macro that can run
I'm in the process of designing a fairly complex system. One of our primary
I'm designing a fairly small web application which will run on a Sun application
I'm designing (and developing) web software that will allow the general public to sign
In a DB I'm designing, there's one fairly central table representing something that's been
I'm currently designing a complex website that needs to support Undo and Notifications for
I'm designing a long running process, triggered by a Django management command, that needs
I am designing a table in the database which will store log entries from
I'm working on a fairly complex project, a custom encryption routine if you will
We are currently designing a business application that has two primary requirements for it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.