PageRank



轉移公告

計劃把 http://blog.hoamon.info/ 文章全部轉移至 http://www.hoamon.info/blog/ 這裡,而本 Blogger 站台的文章近 500 篇,我預計在 2014-12-31 前移轉完畢,完成後 http://blog.hoamon.info/ 將只作代轉服務,一律把舊連結如 http://blog.hoamon.info/index.html 轉成 http://www.hoamon.info/blog/index.html ,敬請舊雨新知互相走告。

新文章只發佈在 http://www.hoamon.info/blog/

何岳峰 敬上

2007年5月8日 星期二

Use django's models without running web server

>>This article also posts on django-users<<

Because i can't find related post here, so i post this example. maybe someone else need.

Sometimes, you just want to use the useful models of django without thinking any sql statement.
Maybe you want to run a script to know which user's birthday in your database is today,
this little job should not run in the view.py. It should run in the cron table of linux os every day,
so that your users can receive birthday card from your system.

this example is so easy, all you know is loading the settings.py correctly.
Your script must place in your django project's directory.

script example:

1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3
4 import settings
5 from django.core.management import setup_environ
6 setup_environ(settings)
7 # before do something else,
8 # the there lines above here is your first operation.
9
10 from django.db.models import get_model
11 from datetime import date
12
13 if __name__ == '__main__':
14
15 # the 'tmp' is your project's name,
16 # and this script must place in the 'tmp' directory.
17 # the 'person' is your model's name.
18 Person = get_model('tmp', 'person')
19
20 # when you get the model,
21 # you can use it just like you use it in the view.py
22 P = Person(name='hoamon', birth=date(1977, 10, 18))
23 P.save()
24
25 for p in Person.objects.all():
26 if p.birth == date(1977, 10, 18):
27 print p.name
28
29 for p in Person.objects.filter(birth=date(1977, 10, 18)):
30 print p.name

now you can put /somepath/tmp/birthday.py in the cron table.

1 則留言:

  1. After i posted this comment on django-users, i found the keyword is "use models outside django", not "without web server".

    回覆刪除

注意:只有此網誌的成員可以留言。

Related Posts Plugin for WordPress, Blogger...