0

I am generating a generic list right now, with the following code:

views.py

class ServiceReportIndex(LoginRequiredMixin, ListView):
    model = TblServiceRecords
    context_object_name = 'all_servicereports'
    login_url = 'login'
    template_name = 'servicereport/servicereport_index.html'

    def get_context_data(self, **kwargs):
        context = super(ServiceReportIndex, self).get_context_data(**kwargs)
        context['companies'] = TblCompanies.objects.all()
        return context

In my template, I want to generate a URL using both of the models. The TblServiceRecords model contains a column that references the company_id, which is the primary key of the appropriate company in the TblCompanies model. I want to use the company_name from the Companies model in my list view. How would I go about doing that? I'm sure it's simple but I can't seem to get my url tags done correctly.

                <div class="col-sm-4">
                  <p>Company Name</p>
                    {% for servicereport in all_servicereports %}
                    <p><a href="{% url 'servicereport:servicereport_detail' servicereport.sr_id %}">{% for servicereport.company_id in companies.company_id %} {{ companies.company_name }} {% endfor %}</a></p>
                    {% endfor %}
                </div>

Also, how can I be sure my views.py is set up correctly for multiple model functionality? I ask because if I put

            {% for company_name in companies %}
                {{companies.company_name}}
            {% endfor %}

In my template, nothing comes up, but there are no errors either.

1 Answer 1

2

Probably you cannot see companies bacause of this:

{{companies.company_name}}

companies is queryset and it does not have company_name property.

Try this:

{% for company_name in companies %}
    {{company_name.company_name}}
{% endfor %} 
Sign up to request clarification or add additional context in comments.

5 Comments

Yep that's right. I am pulling data from the second model correctly. Now I just need help figuring out how to put in the correct TblCompanies.company_name referencing the TblCompanies.company_id with TblServiceRecords.company_id. In other words, if the service record lists TblServiceRecpords.company_id of 123, I look up the company_name from the TblCompanies model with the TblCompanies.company_id of 123.
@Addohm is company_id ForeignKey field in TblCompanies model?
@Addohm - If you have another question then you should ask it as a new question complete with what you have tried and researched. If this answer solved this question, you should consider accepting it.
No, due to conversation constraints both tables have their own primary keys. The company_id is the primary key of the TblCompanies model. The servicerecord_no is the primary key of the TblServiceRecords model.
@Sayse my original question is unanswered. I only added the second question while continuing to troubleshoot this issue. I already upvoted his response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.