0

I have a django app where I am trying to access some Postgres functions using django. I was following this method to achieve this(though the database used in the site is mysql).

I was able to create the function successfully and it worked fine when i was trying it using sql commands.

This is how my model looks:

class Results(models.Model):
    test_type_id = models.IntegerField(null=True, blank=True)
    test_id = models.CharField(max_length=12, null=True, blank=True)
    test_name = models.CharField(max_length=50, null=True, blank=True)
    YR = models.IntegerField(null=True, blank=True)
    @staticmethod

    def summaryrosterbycampus(testtypeid, testid):
        cur = connection.cursor()
        cur.callproc('summaryrosterbycampus',[testtypeid, testid])
        results = cur.fetchall()
        cur.close()
        return [Results(*row) for row in results]

and i am trying to access this from the shell as follows:

>>> from reports.models import *
>>> data = Results.summaryrosterbycampus(request.GET[1,'201403MAME04'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'request' is not defined

Where am i going wrong?

1
  • request is available only when you have a web request/response. That is django adding it for you. It is not available in the shell. Commented Aug 14, 2014 at 20:52

1 Answer 1

1

You're trying to do stuff intended to be done in a view (which passes in a request object) in your shell (which doesn't have a request object). I think you want something like:

exampleResult = Results.objects.all()[0] # or some other way to get a result to give you the test_type_id and test_id values to pass
data = Results.summaryrosterbycampus(exampleResult.test_type_id,exampleResult.test_id)
Sign up to request clarification or add additional context in comments.

5 Comments

From the link you mentioned, note that he says "With the business end of this out of the way I can now perform searches in a view as follows:" and then does "results = Document.search(request.GET['search_string']) "
all i want to do is access the function from django and pass the parameters. it worked when i did this in sql: """select * from summaryrosterbycampus(1,'201403MAME04');""" ...... so how do i pass parameters? is there another way of accessing functions?
data = Results.summaryrosterbycampus(1,'201403MAME04') should work
hi..your suggestion worked. But somehow the values i am getting are lists. Can i get the data as objects from the table?
You should be getting a list of Result objects. Can you post an edit showing what 'print data' returns after the function call?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.