Chams Agouni
Chams Agouni

Reputation: 374

Save form with user id Django

To make my question simple I have A Form That user can upload their cv's into databse.

My forms.py

class resume_upload(forms.ModelForm):
    cv = forms.FileField(required = True)
    job_title = forms.CharField(required = True)

    def save(self, commit=False):
       cvs = super(resume_upload, self).save(commit=False)
       cvs.cv = self.cleaned_data['cv']
       cvs.job_title = self.cleaned_data['job_title']

       if commit:
           cvs.save()


    class Meta:
        model = Cv
        fields = ('cv', 'job_title',)

My models.py

class Cv(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    cv = models.FileField(upload_to='cvs', default='', validators=[validate_file_extension])
    job_title = models.CharField(max_length=100, default='')

    def __str__(self):
        return self.job_title

and my views.py

def upload_resume(request):
if request.method == 'POST':
    form = resume_upload(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        return redirect('/')
    else:
        messages.error(request,"Oops! That didn't work. Please try again")
else:
    form = resume_upload()
return render(request, 'upload_resume.html',{'form':form,})

And the problem is it redirect to home page but i cant find object in admin panel

Upvotes: 1

Views: 1382

Answers (1)

scharette
scharette

Reputation: 9977

You're overriding the save method like so,

 def save(self, commit=False):
   cvs = super(resume_upload, self).save(commit=False)
   cvs.cv = self.cleaned_data['cv']
   cvs.job_title = self.cleaned_data['job_title']

   if commit:
       cvs.save()

You're therefore passing a an optional parameter commit which is False by default.

You'll need to provide a True parameter when calling save if you want it to actually be saved.

form.save(commit=True)

EDIT

Also, there seems to be a problem with your code. This is probably what you intended,

def upload_resume(request):
    if request.method == 'POST':
        form = resume_upload(request.POST, request.FILES)
        if form.is_valid():
            cv_form = form.save() # commit is False in this case.
            cv_form.user = request.user
            form.save(commit=True)
            return redirect('/')
        else:
            messages.error(request,"Oops! That didn't work. Please try again")
    else:
        form = resume_upload()
    return render(request, 'upload_resume.html',{'form':form,})

Basically here you're patching the user to the form before it is actually saved to the database. This is exactly the purpose of your boolean switch commit.

EDIT 2

To overcome your 'NoneType' object has no attribute 'user' error, you would need to add this in your save override to return your object.

def save(self, commit=False):
   cvs = super(resume_upload, self).save(commit=False)
   cvs.cv = self.cleaned_data['cv']
   cvs.job_title = self.cleaned_data['job_title']

   if commit:
       cvs.save()

   return cvs

Upvotes: 4

Related Questions