0

I'm trying to create a category for scraped data that isn't grouped but I get this error. I'm wondering if there is a way I can get around it.

Traceback (most recent call last):
  File "C:\Users\MUHUMUZA IVAN\Desktop\JobPortal\test.py", line 128, in <module>
    the_category = Category.objects.get(title="Others")
  File "C:\Users\MUHUMUZA IVAN\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line 82, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\MUHUMUZA IVAN\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 397, in get
    raise self.model.DoesNotExist(
jobapp.models.Category.DoesNotExist: Category matching query does not exist.
for test_job in final_jobs:

    if 'Manager' in test_job['title']:
        the_category = Category.objects.get(title='Manager')
    elif 'Engineer' in test_job['title']:
        the_category = Category.objects.get(title= 'Engineer')
    elif 'Architect' in test_job['title']:
        the_category = Category.objects.get(title='Architect')
    else:
        the_category = Category.objects.get(title="Others")

I know it says there is no query matching Others, how can I correct this.

Muhumuza
  • 49
  • 8
  • Where do final_jobs come from? – Hossein Asadi Jul 11 '22 at 11:55
  • From another website that I'm scraping data from – Muhumuza Jul 11 '22 at 12:10
  • Does this answer your question? [Create if doesn't exist](https://stackoverflow.com/questions/8766222/create-if-doesnt-exist) – Abdul Aziz Barkat Jul 12 '22 at 04:14
  • Unfortunately, it doesn't. I'm sort of trying to filter if a certain parameter exists in test_job then I create a category. then for whatever hasn't been grouped anywhere else, I want to create a category for them. That's where I'm failing at. I hope this makes my explanation a little clear. – Muhumuza Jul 12 '22 at 10:30
  • What you describe is exactly the question linked above... you want to either get a `Category` object where the title is "Others" if it exists or create it and get it if it doesn't... – Abdul Aziz Barkat Jul 13 '22 at 13:36

1 Answers1

2

Instead of using Category.objects.get(title="...") in each if/elif block you can choose one of these methods to get related the_category object:

Custom function with handled DoesNotExist exception:

def get_category_object(title):
    try:
        the_category = Category.objects.get(title='...')
    except Category.DoesNotExist:
        the_category = None

get_object_or_404() function:

from django.shortcuts import get_object_or_404

the_category = get_object_or_404(Category, title='...')

.get_or_create() method:

the_category, created = Category.objects.get_or_create(title='...')

.filter() and .first() methods:

the_category = Category.objects.filter(title='...').first()
Javad
  • 2,033
  • 3
  • 13
  • 23
  • i nested the if/elif blocks within the try and except and specified the error that was being thrown and i think its working well – Muhumuza Jul 24 '22 at 18:42