Abhishek
Abhishek

Reputation: 3068

Trying to access Postgres function from django

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?

Upvotes: 0

Views: 376

Answers (1)

Foon
Foon

Reputation: 6458

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)

Upvotes: 1

Related Questions