After logging in, my users complete a four forms and a pdf is generated containing the data from the forms. I can successfully do this and save the pdf, but dont know how to attach the generated pdf to the user. I usually save form data in an if request.method == 'POST':
statement in the view.py file, but the pdf isnt being sent through POST, it is instead generated and saved within a function called after if request.method == 'POST':
so I guess i need a way to attach it to the users pdf model within that function?
Also, the questionnaire is quite large so I broke it into four separate forms and four seperate models each with a one-to-one relationship with the user. Is this good practice or should I create one large model and break it up using four separate forms?
utils.py
def generate_questionnaire_pdf(request):
# get user
user = request.user
# create pdf
pdf = SimpleDocTemplate(settings.MEDIA_ROOT+'\pdfs\\questionnaire_pdfs\\user_%s.pdf' %user.id)
[..cont..]
pdf.build(elems, onFirstPage=partial(pageSetup, title=title, user=user), onLaterPages=pageSetup)
form.py
class QuestionnairePdfForm(forms.ModelForm):
class Meta:
model = QuestionnairePdf
fields = ['questionnaire_pdf']
model.py
class QuestionnairePdf(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
questionnaire_pdf = models.FileField(upload_to='pdfs\\questionnaire_pdfs')
view.py
def final_question(request):
if request.method == 'POST':
form = FinalQuestionForm(request.POST, request.FILES, instance=request.user.finalquestion)
if form.is_valid():
form.save()
# generate pdf containing all answers to questionnaire
utils.generate_questionnaire_pdf(request)
Thank you.