当我做python manage.py help
[myapp]
update_overall_score
update_periodic_score
列出了我的自定义命令,我可以在其中运行update_periodic_score
,python manage.py update_periodic_score
但是当我尝试另一个命令时,我得到了Unknown command: 'update_overall_score'
错误。
会有什么问题?这两个文件都放在所有myapp/management/commands
目录__init__.py
中的目录中。
这是我的 update_overall_score.py,
from django.core.management.base import BaseCommand, CommandError
from myapp.models import Users
class Command(BaseCommand):
"""
Updates the overall_score field of every user.
"""
def handle(self, *args, **options):
all_users = Users.objects.all()
try:
for user in all_users:
likes = user.likes_received.count()
stars = user.stars_received.count()
user.overall_score = likes + stars
user.save()
except Exception, e:
print e
return
print "Updated Overall Score."
return