Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name 'IntegrityError' is not defined - why can't I used 'except: IntegrityError' on this script?

I'm running a script from inside a django application at the very root of the app_name folder; as in app_name>app_name.

At the top of the script I have this:

import os
import sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE','versal.settings')
import django
django.setup()

I'm trying to handle an integrity error - my slugs need to be unique, so when one isn't unique, I want to handle the integrity error by adding a '1' to the slug.

        try:
            podcast_instance.slug = slugify(parsed_podcast.title)
            podcast_instance.save()
            podcast_saves += 1
        except IntegrityError:
            podcast_instance.slug = slugify(parsed_podcast.title + '1')
            podcast_instance.save()
            podcast_saves += 1

When I run this code, I get this:

Traceback (most recent call last):
  File "podcasts.py", line 166, in <module>
    podcasty()
  File "podcasts.py", line 159, in podcasty
    submit(podcast_objects,podcast_rss)
  File "podcasts.py", line 73, in submit
    except IntegrityError as e:
NameError: name 'IntegrityError' is not defined
like image 784
phil0s0pher Avatar asked Apr 18 '19 11:04

phil0s0pher


1 Answers

You need to import it before using it.

from django.db import IntegrityError
like image 109
gdef_ Avatar answered Nov 15 '22 20:11

gdef_