3 Replies - 408 Views - Last Post: 20 August 2019 - 12:51 PM Rate Topic: -----

#1 ryoma123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 19

Having the problem with multiple object return

Posted 20 August 2019 - 10:32 AM

For my project i am doing a resume parser. When i upload one resume, it was able to extract those data. When i upload more resume i get this error, i understand it does matter with get() or filter().

I have tried changed my user details
user_detail = UserDetails.objects.get(user=user)
from get() to filter(), although it resolve the multiple object error, but it having another issue is my result extract nothing, no result show at all. It means it doesn't extract any data from my resume although upload successful

def homepage(request):
    if request.method == 'POST':
        user = User.objects.get(id=1)
        UserDetails.objects.filter(user=user).delete()
        Competencies.objects.filter(user=user).delete()
        MeasurableResults.objects.filter(user=user).delete()
        Resume.objects.filter(user=user).delete()
        ResumeDetails.objects.filter(resume__user=user).delete()
        file_form = UploadResumeModelForm(request.POST, request.FILES)
        files = request.FILES.getlist('resume')
        if file_form.is_valid():
            for file in files:
                try:
                    user = User.objects.get(id=1)

                    # saving the file
                    resume = Resume(user=user, resume=file)
                    resume.save()

                    # extracting resume entities
                    parser = resume_parser.ResumeParser(os.path.join(settings.MEDIA_ROOT, resume.resume.name))
                    data = parser.get_extracted_data()

                    # User Details
                    # resume.name          = data.get('name')
                    # resume.email         = data.get('email')
                    # resume.education     = get_education(data.get('education'))
                    user_details = UserDetails()
                    user_details.user           = user
                    user_details.name           = data.get('name')
                    user_details.email          = data.get('email')
                    user_details.mobile_number  = data.get('mobile_number')
                    user_details.skills         = ', '.join(data.get('skills'))
                    user_details.years_of_exp   = data.get('total_experience')
                    user_details.save()

                    for comp in data.get('competencies'):
                        competencies = Competencies()
                        competencies.user       = user
                        competencies.competency = comp
                        competencies.save()

                    for mr in data.get('measurable_results'):
                        measurable_results                   = MeasurableResults()
                        measurable_results.user              = user
                        measurable_results.measurable_result = mr
                        measurable_results.save()

                    # Resume Details
                    resume_details          = ResumeDetails()
                    resume_details.resume   = resume
                    resume_details.page_nos = data.get('no_of_pages')
                    resume_details.save()

                    # resume.experience    = ', '.join(data.get('experience'))
                    # measurable_results.append(data.get('measurable_results'))
                    # resume.save()
                except IntegrityError:
                    messages.warning(request, 'Duplicate resume found:', file.name)
                    return redirect('homepage')

            resumes = Resume.objects.filter(user=User.objects.get(id=1))
            user_detail = UserDetails.objects.get(user=user)
            messages.success(request, 'Resumes uploaded!')

            overall_score = 0

            competencies = data.get('competencies')
            measurable_results = data.get('measurable_results')

            if competencies and measurable_results:
                overall_score = competencies.get('score') + measurable_results.get('score')

            if competencies:
                context = {
                    'resumes': resumes,
                    'competencies': competencies,
                    'measurable_results': measurable_results,
                    'no_of_pages': data.get('no_of_pages'),
                    'total_experience': data.get('total_experience'),
                    'user_details': user_detail,
                    'overall_score': overall_score
                    }
            else:
                context = {
                    'resumes': resumes,
                    'competencies': [],
                    'measurable_results': [],
                    'no_of_pages': data.get('no_of_pages'),
                    'total_experience': data.get('total_experience'),
                    'user_details': user_detail,
                    'overall_score': overall_score
                    }
            return render(request, 'base.html', context)
    else:
        form = UploadResumeModelForm()
    return render(request, 'base.html', {'form': form})



This is my class model
class UserDetails(models.Model):
    user            = models.ForeignKey(User, on_delete=models.CASCADE)
    name            = models.CharField('Name', max_length=255, null=True, blank=True)
    email           = models.EmailField('Email', max_length=255, null=True, blank=True)
    mobile_number   = models.IntegerField('Mobile Number', null=True, blank=True)
    skills          = models.CharField('Skills', max_length=1000, null=True, blank=True)
    years_of_exp    = models.FloatField('Experience', null=True, blank=True)

    def __str__(self):
        return self.user.get_full_name()



In my project, i am expected can upload more resume and extract all of it information successfully, but i am stuck now, i can't find the solution. I am new to python and also django, anyone able help me to solve it? Your help is much appreciated and sorry for my poor English.

Is This A Good Question/Topic? 0
  • +

Replies To: Having the problem with multiple object return

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5457
  • View blog
  • Posts: 14,406
  • Joined: 18-April 07

Re: Having the problem with multiple object return

Posted 20 August 2019 - 12:24 PM

Well things I am noticing is that you get the user on line 3 but then multiple times on line 13 and yet again on 62. You only really need to do it once on line 3 because you want to fetch the user one time and use them multiple times as you associate the resumes with them. For instance I ask your name when you first sit down and not again after I look at each resume right?

Now you are getting user with ID 1, eventually that will be the ID of the user making the upload right? So make sure you change that when required.


Can you show us the error messages it is giving back? The stack trace would be nice to see where exactly it is failing. I am going to guess it is because you are trying to save the same primary or unique column value again. The error messages should give you a hint
Was This Post Helpful? 0
  • +
  • -

#3 ryoma123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 19

Re: Having the problem with multiple object return

Posted 20 August 2019 - 12:39 PM

View PostMartyr2, on 20 August 2019 - 12:24 PM, said:

Well things I am noticing is that you get the user on line 3 but then multiple times on line 13 and yet again on 62. You only really need to do it once on line 3 because you want to fetch the user one time and use them multiple times as you associate the resumes with them. For instance I ask your name when you first sit down and not again after I look at each resume right?

Now you are getting user with ID 1, eventually that will be the ID of the user making the upload right? So make sure you change that when required.


Can you show us the error messages it is giving back? The stack trace would be nice to see where exactly it is failing. I am going to guess it is because you are trying to save the same primary or unique column value again. The error messages should give you a hint


Traceback:

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\PycharmProjects\ResumeParser\resume_parser\parser_app\views.py" in homepage
  86.             user_detail = UserDetails.objects.get(user=user)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py" in get
  412.             (self.model._meta.object_name, num)

Exception Type: MultipleObjectsReturned at /
Exception Value: get() returned more than one UserDetails -- it returned 3!



So i did change my user details from get to filter. However after i changed it, it wouldn't able to extract any information from the resume, refer to the attached photo pic2.
Was This Post Helpful? 0
  • +
  • -

#4 ryoma123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-August 19

Re: Having the problem with multiple object return

Posted 20 August 2019 - 12:51 PM

Posted Image

https://i.imgur.com/6lTE88F.png
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1