我有以下模型屬性。所以我增加了一個@property「is_current」:Django的訪問模式,從經理
@property
def is_current(self):
today = date.today()
if self.date_in and self.date_out:
if self.date_in <= today and self.date_out >= today:
return True
if self.date_in and not self.date_out:
if self.date_in <= today:
return True
當試圖但是調用從過濾器的屬性在我的views.py,我得到以下錯誤,但:*無法解析關鍵字「is_current」到現場。選擇是:date_in,date_out,id,patient,room *
然後我想我可以用Manager來做到這一點。所以我增加了一個經理:
class Hospitalization(models.Model):
def get_query_set(self):
today = date.today()
if self.date_in and self.date_out:
return qs.filter(date_in__lte=today, date_out__gte=today)
if self.date_in and not self.date_out:
return qs.filter(date_in__lte=today)
但是,這並不工作之一:* AttributeError的:「HospitalizationManager」對象有「date_in」沒有屬性*
是什麼Django的推薦的方式解決這一問題?
是'如果真的self.date_in'必要的,如果date_in不能爲空的定義是什麼? – mariodev
它不是。 :-)使用Q對象我能夠實現我想要的。謝謝@asermax。 – SaeX