deadlock
deadlock

Reputation: 7330

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?

Upvotes: 0

Views: 160

Answers (1)

karthikr
karthikr

Reputation: 99680

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> 

Upvotes: 5

Related Questions