如何查询与外键相关的文件字段

How do Query File Field That is related foreign key

大家好,我一直在尽我所能查询外键中的文件,但它不起作用 django 抛出一个错误,我已经尝试了很长时间,现在我刚刚给出了,现在我又回来了需要帮助!

    from __future__ import unicode_literals

from django.db import models


class Examination(models.Model):
    examination_name = models.CharField(max_length=50, unique=True)

    class Meta:
        verbose_name='examination'
        verbose_name_plural='examinations' 

    def __unicode__(self):
        return self.examination_name

class YearAndExaminationName(models.Model):
    exam_name = models.ForeignKey(Examination)
    examination_year = models.CharField(max_length=4)


    class Meta:
        verbose_name='year and examination name'
        verbose_name_plural='year and examination names'

    def __unicode__(self):
        return self.examination_year


class PastQuestion(models.Model):
    year_and_exam_name = models.ForeignKey(YearAndExaminationName)
    file_name = models.CharField(max_length=40, default='self')
    file = models.FileField(upload_to='uploads/', max_length=100,)
    posted_by = models.CharField(max_length=40)


    def __unicode__(self):
        return self.file_name

我打算做的是通过 id 或 pk 从视图中引用来获取 pdf 以显示文件,但是当我搜索 Whosebug 或 google 时,我得到的是如何将 html 转换为 pdf。

我的观点

def jamb_detail(request):
    instance = get_object_or_404()
    context = {
    "title":"Jamb Past Question",
    "instance":instance.year_and_exam_name,
    }
    return render(request, 'past_question/jamb_detail.html', context)

views.py

def pdf_view(request):

    # get file path by querying model and then
    with open('/path/to/my/file.pdf', 'rb') as pdf:
        response = HttpResponse(pdf.read(), mimetype='application/pdf')
        response['Content-Disposition'] = 'inline;filename=some_file.pdf'
        return response
    pdf.closed