除了编写自定义django-admin命令which is pretty well documented之外,我希望能够覆盖现有命令,例如manage.py loaddata (fixture)
,这样我就可以添加一些进一步的处理加载到我的数据库中。
我想我必须编写一个自定义命令,首先调用' loaddata'然后做自己的治疗。有这么好的办法吗?
有更好的解决方案吗?
答案 0 :(得分:8)
感谢摩西与其他SO答案的链接,我最终设法编写了一个模板,以便对loaddata
命令进行额外处理。这是一个解释诀窍的片段:
"""
Additional treatment for the loaddata command.
Location example: project/app/management/commands/loaddata.py
"""
from django.core.management.base import BaseCommand, CommandError
from django.core.management.commands import loaddata
class Command(loaddata.Command):
def handle(self, *args, **options):
super(Command, self).handle(*args, **options)
self.stdout.write("Here is a further treatment! :)")
不要忘记将您的应用程序放在INSTALLED_APPS配置的顶部