2014-03-05 158 views
0

我想創建一個模型,應該只接受從今天起15天的日期,並存儲開始時間和結束時間,並在開始時間之前輸入結束時間時顯示錯誤。Django模型日期時間字段

+0

所以......開始時間應不早於今天+15天? – Brandon

+0

聽起來像一個表單驗證任務。你真的想把它放在模型層面上嗎? – alecxe

回答

0

模型清潔功能是你要找的。

這是正確的,從Django文檔的例子開始:

import datetime 
from django.core.exceptions import ValidationError 
from django.db import models 

class Article(models.Model): 
    ... 
    def clean(self): 
     # Don't allow draft entries to have a pub_date. 
     if self.status == 'draft' and self.pub_date is not None: 
      raise ValidationError('Draft entries may not have a publication date.') 
     # Set the pub_date for published items if it hasn't been set already. 
     if self.status == 'published' and self.pub_date is None: 
      self.pub_date = datetime.date.today() 

進一步的細節在這裏閱讀:https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects

相關問題