1

On a Django app, I'm using cx_Oracle to display a roster of sports players:

def get_players(self, sport_code='', term=0):
"""

:rtype: object
"""
con = cx_Oracle.Connection(settings.BANNER_CONNECTION_URL)
cursor = con.cursor()
query = 'select PREF_NAME, CLASS, ELIGIBLE_HOURS, CHECKED_IN, SEASONS_USED, MAJR1, NVL(MINR1, Null), CA_EMAIL, ID from swvsprt_web where ACTC = \'%s\' AND term = \'%s\' ORDER BY PREF_NAME' % (
    sport_code, term)
cursor.execute(query)
players = cursor.fetchall()
cursor.close()
con.close()
return players

I want minor to just be blank if they don't have a minor

NVL(MINR1, Null)

But I can't quite get NVL to behave. "Null" makes it print the word "None." If I do

NVL(MINR1, 0)

it will display a 0, but if I try any of the following, they crash the site with a 500 error:

NVL(MINR1, )

NVL(MINR1, '')

NVL(MINR1, "")

Can we use NVL to make it show null values as just nothing?

MT0
  • 143,790
  • 11
  • 59
  • 117

2 Answers2

0

In Python, the 'null' object is the singleton None

If you want to display something else, you should post-process the data, and replace values of None in that field with an empty string.

A better alternative might be to just leave the values of None as is in your backend representation. This is more representative of not having a minor than an empty string.

Then, on your frontend template, you can check for a null value, and display an empty string if MINR1 is None.

roob
  • 2,419
  • 3
  • 29
  • 45
  • Thank you. So try handling it on the template layer instead of model layer. I'm going to give this a shot. – Kevin Celebi Sep 01 '17 at 19:10
  • @KevinCelebi did this work for you? If so, you can vote up/ accept my answer. Otherwise, I'm happy to help you continue to work through this. – roob Sep 05 '17 at 18:02
  • I would love to upvote this but Stackoverflow won't let me do anything since I'm too new! Thank you for the help though, the template layer is a lot more powerful than I knew. – Kevin Celebi Sep 06 '17 at 20:00
0

There's an analogy of Oracle's NVL in Django templates:

<td><span class="entityName">{{ attr.name|default_if_none:"" }}</span></td>

if attr.name is None an empty string will be output.

Here's the documentation.

Nick Legend
  • 789
  • 1
  • 7
  • 21