Skip to main content
1 de 2

Multiple queries in one view Django

Hi I need some help with a little problem, I am new to Django and have not mastered it properly yet. I am trying to render a view in pdf, to put you in situation I am going to explain what I need. It is an application to manage properties. Obviously each client can have multiple properties. The problem is that when listing the properties of a client it only shows me one record.

Here my view:

def pdf_generation(request, *args, **kwargs):
pk = kwargs.get('pk')
client = Client.objects.get(pk=pk)
farm = Farm.objects.filter(pk=pk)
context = {
    'client': client,
    'farm': farm,
}
html_string = render_to_string('client_pdf.html', context)
html = HTML(string=html_string, base_url=request.build_absolute_uri())
pdf = html.write_pdf(
    stylesheets=[CSS(settings.STATIC_ROOT + '/css/pdf.css')])
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename="export.pdf"'
return response

Here my url

    path('export/<int:pk>', views.pdf_generation, name='client_export_id'),

Here my html code for client

        <tbody>
            <tr>
                <th scope="row">{{ client.id }}</th>
                <th scope="row">{{ client.name }}</th>
                <th scope="row">{{ client.nif }}</th>
                <th scope="row">{{ client.technical }}</th>
                <th scope="row">{{ client.phone }}</th>
                <th scope="row">{{ client.record|date:"j F, Y" }}</th>
            </tr>
        </tbody>

And finally my html code for farm

        <tbody>
            {% for obj in farm %}
            <tr>
                <th scope="row">{{ obj.id }}</th>
                <th scope="row">{{ obj.manager }}</th>
                <th scope="row">{{ obj.farm }}</th>
                <th scope="row">{{ obj.town }}</th>
                <th scope="row">{{ obj.place }}</th>
                <th scope="row">{{ obj.production }}</th>
            </tr>
            {% endfor %}
        </tbody>