2015-04-17 68 views
0

我已經編輯了一些代碼從這裏In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?對象「項目」沒有屬性「ro_field」

def clean_sku(self): 
     instance = getattr(self, 'instance', None) 
     if instance and instance.pk: 
      return instance.sku 
     else: 
      return self.cleaned_data['sku'] 

而不是上運行實例的SKU屬性的代碼。我想循環多個屬性

readonly = ['name', 'description', 'deadline'] 

def clean(self): 
     instance = getattr(self, 'instance', None) 
     for ro_field in self.readonly: 
      if instance and instance.pk: 
       return instance.ro_field 
      else: 
       return self.cleaned_data[ro_field] 

這給我錯誤的標題。我怎樣才能得到解釋解釋instance.nameinstance.descriptioninstance.deadline而非instance.ro_field

回答