![](/img/trans.png)
[英]How can I create a template .csv file that users can fill in before running my script?
[英]How can I create a script where it will print all email addresses of my Django users into a small .txt file?
我有大约6000个用户在使用Django应用。 我最近发现了Mailchimp,我想向所有这些用户发送有关该网站关闭几天的消息。
而不是复制和粘贴每一封电子邮件(大约6000)。 我想知道是否存在python脚本或可以将我的应用程序中每个django用户的所有电子邮件地址写成一个小的.txt的内容。
我该怎么做呢?
您可以编写管理命令来实现相同目的。
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
class Command(BaseCommand):
def handle(self, *args, **options):
user_emails = User.objects.values_list('email', flat=True)
with open('myfile.txt', 'w+') as fh:
fh.write(", ".join(list(user_emails)))
并在命令行中:
./manage.py <managementcommand_filename>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.