简体   繁体   中英

How can I create a script where it will print all email addresses of my Django users into a small .txt file?

I have about 6000 users using my Django app. I recently discovered Mailchimp and I would like to push out a message to all of these users about the website going down for a few days.

Instead of copying and pasting each and every single email (about 6000). I wanted to know if there is a python script or something that can write out all the email addresses of every single django user in my app into a small .txt.

How can I go about doing this?

You can write a management command to achieve the same.

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)))

and in the command line:

./manage.py <managementcommand_filename> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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