文章詳情頁
python - django的model字段在保存的時(shí)候做預(yù)處理怎么辦?
瀏覽:171日期:2022-09-04 18:00:00
問題描述
比如這個(gè)model:
class Book(Model): publish_date = DateField()
但是在保存時(shí),用戶輸入數(shù)據(jù)是:
book1 = Book(publish_date=’20171001’)
我希望這個(gè)publish_date能夠接受字符串輸入,自動(dòng)轉(zhuǎn)為Date類型存入數(shù)據(jù)庫。這個(gè)應(yīng)該怎么處理呢?我現(xiàn)在這么處理,但是沒用,還是報(bào)錯(cuò)說只接受2017-10-01格式的字符串
from dateutil.parser import parseclass Book(Model): publish_date = DateField() def save(self, *args, **kwargs):self.publish_date = parse(self.publish_date)return super(Book, self).save(*args, **kwargs)
問題解答
回答1:哦,我這個(gè)方法是對(duì)的。沒問題。自己測試用例寫錯(cuò)了:(
相關(guān)文章:
1. 如何分別在Windows下用Winform項(xiàng)模板+C#,在MacOSX下用Cocos Application項(xiàng)目模板+Objective-C實(shí)現(xiàn)一個(gè)制作游戲的空的黑窗口?2. angular.js - angularjs的自定義過濾器如何給文字加顏色?3. 關(guān)于docker下的nginx壓力測試4. javascript - 關(guān)于json中獲取多個(gè)key-value對(duì)中多層嵌套key的name5. 關(guān)于Java引用傳遞的一個(gè)困惑?6. android clickablespan獲取選中內(nèi)容7. android - 啟動(dòng)模擬器的,報(bào)“Could not automatically detect an ADB binary……”,要怎么解決?8. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.9. python - TypeError: tryMsgcode() takes exactly 2 arguments (0 given)10. javascript - 最終生成的jsBundle文件壓縮問題
排行榜

熱門標(biāo)簽