Here is my code: In forms.py
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ("title","business_partner","transaction")
widgets = {
'transaction': forms.NumberInput()
}
In views.py
def uploadpdf(request,pk):
project_form = ProjectForm(request.POST)
if project_form.is_valid() and request.FILES:
project_form.instance.user = request.user
project = Project.objects.get(id=pk)
project.title = project_form['title']
project.business_partner = project_form['business_partner']
project.transaction = project_form['transaction']
project.save()
project = Project.objects.get(id=pk)
file_path = None
for file in request.FILES:
file = request.FILES[file]
pdf_file = PDFFile(file=file, filename=file.name)
pdf_file.project = project
pdf_file.save()
if PRODUCTION:
file_path = HOST_NAME '/' str(pdf_file.file)
else:
file_path = HOST_NAME '/media/' str(pdf_file.file)
resp = HttpResponse(f'{{"message": "Uploaded successfully...", "id": "{project.id}","url":"{file_path}","title": "{project.title}"}}')
resp.status_code = 200
resp.content_type = "application/json"
return resp
else:
return reverse("dashboard:homepage")
When I run this, it says like "TypeError: Field 'transaction' expected a number but got <django.forms.boundfield.BoundField object at 0x000001A803935250>."
Looking forward to hearing a good solution.
CodePudding user response:
You want to use the cleaned_data attribute of the form instead of the field itself like so:
project.transaction = project_form.cleaned_data['transaction']
Edit: Note that you should have to do the same for the other fields.