2016-08-01 38 views
0
class Control(models.Model): 
    period = models.DurationField() 
    active = models.BooleanField() 
    device_collection = models.ForeignKey(DeviceSet) 

class DeviceSet(models.Model): 
    name = models.CharField() 
    date_last_control = models.DateField() 

    def get_next_control(self): 
     return self.date_last_control + self.control_actif.period 

    @property 
    def control_actif(self): 
     if not hasattr(self, "_control"): 
      setattr(self, "_control", self.control_set.get(active=True)) 
     return self._control 

有幾個控件與DeviceSet相關聯,但只有一個控件由DeviceSet激活。 當我在列_control中獲取查詢集時,我希望獲得DeviceSet的活動控制權。 我已經嘗試:正在尋找ForeignKey並將其添加到QuerySet中

DeviceSet.objects.annotate(_control = Q(control__active=True)) 

不工作

'WhereNode' object has no attribute 'output_field' 

而且output_field =控制我有以下異常集後:

type object 'Control' has no attribute 'resolve_expression' 

我只是想有一個像prefetch_related與過濾器,但在新列中使用模型的方法_control屬性。

回答

相關問題