简体   繁体   English

Django 使用外键渲染为 PDF

[英]Django render to PDF with foreign key

I want to render to PDF the nome of pavimento on AvariaAdmin , and i really dont know how to do that.我想渲染成PDF的nomepavimentoAvariaAdmin ,我真的不知道该怎么做。

http://prntscr.com/mmm5eo http://prntscr.com/mmm5eo

MY models我的模型

class Avaria(models.Model):

    freguesia = models.ForeignKey(Freguesia, on_delete=models.CASCADE,verbose_name="Freguesia")
    rua = models.ForeignKey(Rua, on_delete=models.CASCADE,verbose_name="Rua")
    porta = models.PositiveIntegerField(verbose_name="Numero de polícia")
    tipo_avaria = models.ForeignKey(Tipo_avaria, on_delete=models.CASCADE,verbose_name="Tipos de avaria")
class Pavimentacao(models.Model):

    avaria = models.ForeignKey(Avaria, related_name='AvariaObjects',on_delete=models.CASCADE)
    pavimento = models.ForeignKey(Pavimento, on_delete=models.CASCADE, verbose_name="Pavimento")
class Pavimento(models.Model):

    nome = models.CharField("Pavimento",max_length=200)

MY DEF (INSIDE OF AvariaAdmin)我的 DEF(在 AvariaAdmin 内部)

def Imprimir(self, request, obj):
    data = {
            'obj':obj
    }
    pdf = render_to_pdf('daa/imprimir/avarias_pdf.html', data)
    if pdf :
        response = HttpResponse(pdf, content_type='application/pdf')
        filename ="Avaria_%s.pdf" %("123451231")
        content = "inline; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        download = request.GET.get("download")
        if download:
                content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        return  response
    return HttpResponse("Not found")

MY HTML我的 HTML

        <table style="width:100%">
                <tr>
                    <th>ID:</th>
                    <th>Freguesia:</th>
                    <th>Rua:</th>  
                    <th>Porta:</th>
                    <th>Tipo avaria:</th>
                    <th>Pavimento:</th>
                 </tr>
                 {% for item in obj %}
                    <tr> 
                        <td>{{item.id}}</td>   
                        <td>{{item.freguesia }}</td>
                        <td>{{item.rua}}</td> 
                        <td>{{item.porta}} </td>
                        <td>{{item.tipo_avaria}} </td>
                        <td>{{item.pavimento}} </td>
                    </tr>
                {% endfor %}    

        </table>

First there's no need for the class Pavimentacao if that's all it contains (but maybe you didn't show us all the model).首先,如果Pavimentacao类包含所有内容,则不需要它(但也许您没有向我们展示所有模型)。 It's just holding a many-to-many relationship between Avaria and Pavimento which you should model directly with a models.ManyToManyField :它只是在AvariaPavimento之间保持多对多关系,您应该直接使用models.ManyToManyField建模:

class Avaria(Model):
    ...
    pavimentos = models.ManyToManyField(Pavimento)

Then in your template, if you want to print all the Pavimento instances:然后在您的模板中,如果您想打印所有Pavimento实例:

<tr>
    ...
    <td>
        {% for pavimento in item.pavimentos.all %}
            {{ pavimento.nome }}<br>
        {% endfor %}
    </td>
</tr>

But if you leave your models as they are now, the template code would be:但是如果你让你的模型保持原样,模板代码将是:

<tr>
    ...
    <td>
        {% for pavimentacao in item.AvariaObjects.all %}  <!-- this is because of the related_name you defined -->
            {{ pavimentacao.pavimento.nome }}<br>
        {% endfor %}
    </td>
</tr>

See how the related_name you gave is wrong?看看你给的related_name是怎么错的? related_name is for the reverse relationship so it should be the name of the model you're defining, not the one you're referring to. related_name用于反向关系,因此它应该是您定义的模型的名称,而不是您所指的模型的名称。 Read the docs to understand reverse relationships.阅读文档以了解反向关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM