Skip to main content

方便快捷的pdf导出工具

Project description

新加PdfAdmin,如有需要导出pdf的admin直接继承PdfAdmin 如需在列表添加查看pdf功能,在list_display中添加'pdf_show'字段

setting.py中

INSTALLED_APPS = [
	----------其他APP
    'pdfExport'
    -----------其他APP
]

admin中

注pdf_fields中可加入的元素

1.filed1和filed2字段各占一行

pdf_fields = ('filed1','filed2')

例图

2.filed1和filed2字段共占一行,filed3单独一行

pdf_fields = (('filed1','filed2'),'filed3')

例图1

3.filed1和filed2字段共占一行,filed3单独一行,title1为filed1和filed2的标题,title2为filed3标题

pdf_fields = (('title1', {('filed1', 'filed2')}), ('title2', {('filed3',)}),'filed4')

例图2

@admin.register(TodoItem)
class TodoItemAdminBase(PdfAdmin):
    list_display = ('title', 'assignee', 'created_at', 'updated_at', 'is_completed', 'pdf_show')
    list_filter = ('created_at', 'updated_at', 'assignee')
    search_fields = ('title', 'assignee')
    # pdf要显示的字段
    # 注:每行最多放两个字段
    pdf_fields = (('组标题', {('title', 'assignee')}), 'handler', ('时间', {('created_at',)}), 'type')
    # pdf标题
    pdf_title = '策略申请表'
    # 要显示所有选项的字段
    option_fields = ['type', ]
    # 左右上方小标题
    left_tip = '编号AAA'
    right_tip = '编号BBB'
    # 如有其他需求,可以自定义pdf_template,继承base_pdf_template.html,添加需要的模块

4.关联表展示,定义pdf_related_fields,下列代码中modelx,model_bs,model_cs为关联字段,自定义标题x,自定义标题b,自定义标题c为关联模型的表格标题,第三个元素同pdf_fields使用方法一样。

class ModelType(models.Model):
    name = models.CharField(max_length=100, verbose_name='名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '选项类型'
        verbose_name_plural = verbose_name


class ModelX(models.Model):
    model_filed1 = models.CharField(max_length=100, verbose_name="模型X字段1")
    model_filed2 = models.CharField(max_length=100, verbose_name="模型X字段2")
    model_filed3 = models.CharField(max_length=100, verbose_name="模型X字段3")
    model_filed4 = models.CharField(max_length=100, verbose_name="模型X字段4")
    model_filed5 = models.CharField(max_length=100, verbose_name="模型X字段5")
    model_filed6 = models.CharField(max_length=100, verbose_name="模型X字段6")
    model_filed7 = models.ForeignKey(ModelType, on_delete=models.CASCADE, blank=True, null=True,
                                          verbose_name="模型X字段7")

    def __str__(self):
        return self.model_filed1


class ModelA(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    modelx = models.ManyToManyField(ModelX, related_name='model_as')

    def __str__(self):
        return self.name


class ModelB(models.Model):
    model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name='model_bs')
    title = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return self.title


class ModelC(models.Model):
    model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name='model_cs')
    title = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return self.title
class ModelAAdmin(PdfAdmin):
    list_display = ('name', 'description', 'pdf_show')  # 在Admin列表页显示的字段
    inlines = [ModelBInline, ModelCInline]  # 在ModelA的编辑页面嵌入ModelB的编辑表单
    # pdf要显示的字段
    pdf_related_fields = [

        ('modelx', '自定义标题x', (('model_filed1', 'model_filed2'),
                  ('组标题1', {('model_filed3', 'model_filed4')}),
                  'model_filed5',
                  ('组标题2', {('model_filed6',)}),
                  'model_filed7')),
        ('model_bs', '自定义标题b', ('title', 'content')),
        ('model_cs', '自定义标题c', ('title', 'content')),
    ]
    option_fields = ('model_filed7',)

    # pdf_related_fields = ('model_bs', 'model_cs')
    pdf_fields = (('name', 'description'),)
    # pdf标题
    pdf_title = 'pdf标题'
    # 要显示所有选项的字段
    # 左右上方小标题
    left_tip = '编号AAA'
    right_tip = '编号BBB'

img_3.png 例图3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qz_pdf_export-0.1.10.tar.gz (548.0 kB view details)

Uploaded Source

Built Distribution

qz_pdf_export-0.1.10-py3-none-any.whl (284.7 kB view details)

Uploaded Python 3

File details

Details for the file qz_pdf_export-0.1.10.tar.gz.

File metadata

  • Download URL: qz_pdf_export-0.1.10.tar.gz
  • Upload date:
  • Size: 548.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for qz_pdf_export-0.1.10.tar.gz
Algorithm Hash digest
SHA256 cd8ae24e7e1e847032bb022c047cd37fdc0799a96f8b14b4b68fc8ec217e27c7
MD5 81a3fb64114d9f7ab1080a780f410013
BLAKE2b-256 66fdb97823ed4889e137f8d1cb495557fe16cb8f40ea73e31c9a6b02e64ff555

See more details on using hashes here.

File details

Details for the file qz_pdf_export-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for qz_pdf_export-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 66179c03001a0b8194eee9a31c0365d1d3287d19eca56d941a43c23d9c354352
MD5 99046bf2e9180c04851aa91e320913f0
BLAKE2b-256 6b09a960c57219ab23235f0064a9c8798c6d221b5bc1f6475f5daac0d6644881

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page