Home > other >  How to save data to user models when using a resume parser in django
How to save data to user models when using a resume parser in django

Time:11-29

I am working on a website whereby users will be uploading resumes and a resume parser script will be run to get skills and save the skills to the profile of the user. I have managed to obtain the skills before saving the form but I cant save the extracted skills now. Anyone who can help with this issue will be highly appreciated. Here is my views file

def homepage(request):
    if request.method == 'POST':
        # Resume.objects.all().delete()
        file_form = UploadResumeModelForm(request.POST, request.FILES, instance=request.user.profile)
        files = request.FILES.getlist('resume')
        resumes_data = []
        if file_form.is_valid():
            for file in files:
                try:
                    # saving the file
                    # resume = Profile(resume=file)
                    resume = file_form.cleaned_data['resume']
                   
                    # resume.save()
                    # resume = profile_form.cleaned_data['resume']
                     # print(file.temporary_file_path())
                    
                    # extracting resume entities
                    # parser = ResumeParser(os.path.join(settings.MEDIA_ROOT, resume.resume.name))
                    parser = ResumeParser(file.temporary_file_path())
                    
                    # extracting resume entities
                    # parser = ResumeParser(os.path.join(settings.MEDIA_ROOT, resume.resume.name))
                    data = parser.get_extracted_data()
                    resumes_data.append(data)
                    resume.name               = data.get('name')
                    resume.email              = data.get('email')
                    resume.mobile_number      = data.get('mobile_number')
                    if data.get('degree') is not None:
                        resume.education      = ', '.join(data.get('degree'))
                    else:
                        resume.education      = None
                    resume.company_names      = data.get('company_names')
                    resume.college_name       = data.get('college_name')
                    resume.designation        = data.get('designation')
                    resume.total_experience   = data.get('total_experience')
                    if data.get('skills') is not None:
                        resume.skills         = ', '.join(data.get('skills'))
                    else:
                        resume.skills         = None
                    if data.get('experience') is not None:
                        resume.experience     = ', '.join(data.get('experience'))
                    else:
                        resume.experience     = None
                    # import pdb; pdb.set_trace()
                    resume.save()
                except IntegrityError:
                    messages.warning(request, 'Duplicate resume found:', file.name)
                    return redirect('homepage')
            resumes = Profile.objects.all()
            messages.success(request, 'Resumes uploaded!')
            context = {
                'resumes': resumes,
            }
            file_form.save()
            return render(request, 'authentication/resume.html', context)
    else:
        form = UploadResumeModelForm()
    return render(request, 'authentication/resume.html', {'form': form})



And here is my models:

# Extending User Model Using a One-To-One Link
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    avatar = models.ImageField(default='default.jpg', upload_to='profile_images')
    bio = models.TextField()
    resume        = models.FileField('Upload Resumes', upload_to='resumes/', null=True, blank=True)
    name          = models.CharField('Name', max_length=255, null=True, blank=True)
    email         = models.CharField('Email', max_length=255, null=True, blank=True)
    mobile_number = models.CharField('Mobile Number',  max_length=255, null=True, blank=True)
    education     = models.CharField('Education', max_length=255, null=True, blank=True)
    skills        = models.CharField('Skills', max_length=1000, null=True, blank=True)
    company_name  = models.CharField('Company Name', max_length=1000, null=True, blank=True)
    college_name  = models.CharField('College Name', max_length=1000, null=True, blank=True)
    designation   = models.CharField('Designation', max_length=1000, null=True, blank=True)
    experience    = models.CharField('Experience', max_length=1000, null=True, blank=True)
    total_experience  = models.CharField('Total Experience (in Years)', max_length=1000, null=True, blank=True)
    

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

I have tried following it step by step with the pdb but when it comes to saving I get an error. Here are some of the errorsSome errors I am getting in pdb

CodePudding user response:

The cause of your error is when you cycle through the files submitted in your resume form, you are trying to save the resume field (remember, resume = file_form.cleaned_data['resume'] ). Presumably you want to be saving a Profile object

In all those lines where you add things to resume from your parsed resume file eg

 resume.name = data.get('name')

just replace them with

file_form.instance.name = data.get('name')

and then

file_form.save() 

at the end.

Also, it seems like you don't need the user to be able to submit multiple resumes. You probably don't need to loop through each file in resumes either.

  • Related