Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin export #840

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

Conversation

Deophius
Copy link
Collaborator

@Deophius Deophius commented Sep 3, 2024

简易的导出功能。

使用了django.core.serializers模块里提供的转化为字符串功能,并将序列化得到的JSON转化为Excel文件,返回给用户。Excel文件中每列的名称来自模型定义中每个field的verbose_name.

Django的序列化默认使用主键作为外键的输出。为了提高可读性,利用了Django的natural key功能,让序列化时可以指定外键的表现形式。例如,通过设置奖池Pool模型的natural key为奖池名称,在导出奖池记录PoolRecord时,其中含有的Pool foreign key列将包括奖池名称字符串,而不是对应Pool对象的id。本PR中包含了一些类的natural key定义,后续可以根据实际使用情况进行维护。

导出功能在设计时只考虑了“导出”,没有考虑从获得的Excel文件恢复数据库对象,所以natural key并不一定能唯一确定对象,没有完全实现Django的要求,这点需要注意。

接下来需要为现有的模型增加自然键的定义,并且尝试增加”选择导出的列“的功能。
有可能需要把导出逻辑放到另一个页面上。
考虑到我们定义natural key只要是为了让导出的excel中外键处理更美观,没有全部加natural_key。
后面如果遇到有导出的是id的情况,可以再补充。
@Deophius Deophius added the need test 尚需用户测试 label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need test 尚需用户测试
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant