import datetime import logger from django.conf import settings from django.db.models import Q from django.core.management.base import BaseCommand from xxx.models import User from xxx.tools import message_client MSEEAGE = u"Hello %s , Welcome to XXX." class Command(BaseCommand): def handle(self, *args, **kwargs): print args print kwargs send_level = args[0].split(',') td = datetime.datetime.now() td0 = datetime.datetime(dt.year, dt.month, dt.day) reg_time = now_day_value + datetime.timedelta(days=-4) query_condition = Q(active='1') & Q(status='y') & Q(registe_time__gte=reg_time) & Q(level__in=send_level) user_list = User.objects.filter(query_condition) for u in user_list: if u.phone: try: msg = MSEEAGE % u.name message_client.send_message(phone_number=eachUser.phone_no, content=msg.encode("gbk")) except Exception, e: logger.error(e.message) logger.error("failed send to %s" % str(u.phone))
比如我们的python 文件名是:batch_send_msg.py
那么我们调用的方法是:python manage.py batch_send_msg 1,2 ;
给等级为1和2的用户发送消息。
和Django 启动的命令(python manage.py runserver 0.0.0.0:8001)使用方式一样