2011年1月22日

python 讀檔

讀檔當然就 open 就好啦,超簡單的有什麼好寫?
不過如果檔案不是 ascii 就....

所以還是要花點功

2011年1月21日

django ModelAdmin form submit

記錄一下 django 裡面處理有 inline formset 的 form 時,
它的 calling sequence

2011年1月12日

python timestamp

python 要做 linux 的 time_t 格式的話

>>> import datetime, calendar
>>> now = datetime.datetime.now()
>>> type(now)
<type 'datetime.datetime'>
>>> calendar.timegm(now.timetuple())
1294833875

django models 與 sql database 的對應

ref: http://www.djangoproject.com/documentation/models/custom_columns/

django 預設在建立 db schema 的時候
會把 table name 設成 {app_label}_{model_name},
然後 foreign key 的那個 column name 會幫你 append 一個 "_id",
另外如果你沒有指定 primary key,它也會幫你加一筆 "id"

django admin site model url

有時候會想 link 到 model 的 change_form 那一頁,
自己兜 url 的話是這樣

class SiteInfo(models.Model):
    CategoryId = models.ForeignKey('SiteCategory', db_column='CategoryId', db_index=True)
    def show_category_in_list(self):
        return u'{3}'\
                 .format(self.CategoryId._meta.app_label,
                         self.CategoryId._meta.object_name.lower(),
                         self.CategoryId.Id,
                         self.CategoryId.__unicode__())
    show_category_in_list.allow_tags = True

順便注意要傳 html content 的話要設一下 allow_tags