如何在Django模板中访问外键? (ListView)

时间:2019-03-01 05:38:06

标签: javascript html css django python-3.x

在我的项目中,Profile模型与Foreign Key实例具有Education关系。这是我的模特:

class Profile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    full_name = models.CharField(max_length=30, null=True, blank=True)
    education = models.ForeignKey(Education, on_delete=models.SET_NULL, null=True, blank=True, related_name="education")

class Education(models.Model):
    degree = models.CharField(max_length=100, null=True, blank=True)
    school = models.CharField(max_length=100, null=True, blank=True)
    edu_start_date = models.DateField(null=True, blank=True)
    edu_end_date = models.DateField(null=True, blank=True)

    def __str__(self):
        return str(self.degree)

现在,使用Django ListView,我无法显示外键数据。我的看法:

class EducationView(CreateView):
    form_class = EducationForm
    pk_url_kwarg = 'pk'
    template_name = "profile_settings.html"

class EducationList(ListView):
    model = Profile
    queryset = Profile.objects.all()
    context_object_name = 'object'
    pk_url_kwarg = 'pk'
    template_name = "profile_settings.html"

模板

{% for obj in profile.education.all %}
    <div class="col-lg-12">
        <h2>{{ obj.degree }}</h2>
        <br>
        <div>
            {{ obj.school }}
        </div>
    </div>
    <br>
{% endfor %}

教育表格将数据保存到数据库中,但是我无法使用模板代码来获取它。

注意:我正在为CreateView和ListView使用单个模板。

2 个答案:

答案 0 :(得分:1)

您需要这样做:

{% for obj in object %}  <!-- as your context_object_name is `object` -->
    <div class="col-lg-12">
        <h2>{{ obj.education.degree }}</h2>  <!-- accessed foreign key -->
        <br>
        <div>
            {{ obj.education.school }}  <!-- accessed foreign key -->
        </div>
    </div>
    <br>
{% endfor %}

更新

如果您在创建视图中使用此模板,请按以下方式更新视图:

class EducationView(CreateView):
    form_class = EducationForm
    pk_url_kwarg = 'pk'
    template_name = "profile_settings.html"

    def get_context_data(self, **kwargs):
        context = super(EducationView, self).get_context_data(**kwargs)
        context['profiles'] = Profile.objects.all()  # I don't want to mix up `object` here. so using profiles
        return context

现在,在ListView和模板中,我们还将更新上下文对象:

# view
class EducationList(ListView):
    model = Profile
    queryset = Profile.objects.all()
    context_object_name = 'profiles'
    template_name = "profile_settings.html"

# template

{% for obj in profiles %}
    <div class="col-lg-12">
        <h2>{{ obj.degree }}</h2>
        <br>
        <div>
            {{ obj.school }}
        </div>
    </div>
    <br>
{% endfor %}

答案 1 :(得分:1)

在ListView中,您将context_object_name指定为“ object”。因此,在模板内部,上下文是指对象。

所以代码看起来像

{% for each_profile in object %}
    {% for obj in each_profile.education.all %}
        <div class="col-lg-12">
            <h2>{{ obj.degree }}</h2>
            <br>
            <div>
                {{ obj.school }}
            </div>
        </div>
        <br>
    {% endfor %}
{% endfor %}