开发者

custom django-admin command: problems with use models and crontab

I created custom django-admin command. I use this tutorial Writing custom django-admin commands

For example, my command:

import os
from home.models import BuildTask

class Command(BaseCommand):
    def handle(self, *args, **options):

        tasks = BuildTask.objects.all()

        os.system("touch /Users/macbook/Desktop/Start.txt")

        if tasks:
            os.system("touch /Users/macbook/Desktop/TasksExist.txt")
        else:
            os.system("touch /Users/macbook/Desktop/TasksNotExist.txt")

        os.system("touch /Users/macbook/Desktop/End.txt")

when i run this command by use terminal:

python manage.py build_task

everything is OK, i get 3 files on Destop.

But when i use crontab,

* * * * *  /usr/bin/python /Users/macbook/builder/manage.py build_task

i just get 1 file on Desktop("Start.txt"). Apparently the script stops job on the line.

if tasks:

Please help me. What's the problem?

Update: /var/mail/macbook:

 File "/Users/macbook/workspace/acodebuilder/home/management/commands/build_apk.py",    
     line 26, in handle
 if tasks:
 File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 113, in 
     __nonzero__
 iter(self).next()
 File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 107, in 
     _result_iter
 self._fill_cache()
 File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 772, in 
     _fill_cache
 self._result_cache.append(self._iter.next())
 File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 273, in 
     iterator
 for row in compiler.results_iter():
 File "/Library/Python/2.7/开发者_如何学编程site-packages/django/db/models/sql/compiler.py", line 680, in
    results_iter
 for rows in self.execute_sql(MULTI):
 File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 735, in 
     execute_sql
 cursor.execute(sql, params)
 File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 34, in 
     execute
 return self.cursor.execute(sql, params)
 File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, 
    in execute
 return Database.Cursor.execute(self, query, params)

 django.db.utils.DatabaseError: no such table: home_buildtask

Why "no such table" ?


You should be able to see the error output of the command (if there was any) by running the mail command. Alternatively, you can append &> ~/Desktop/django-command-error.log to the crontab entry and the error output of the cron job will be saved to django-command-error.log on your desktop.


it's issue caused by SQLite db, We must use MySQL or PGSQL!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜