2009-09-07 88 views
2

我在嘗試控制管理項條目,其中非超級用戶帳戶無法保存具有日期屬性超過2天的ChannelStatus模型輸入。我需要得到用戶,以便我可以檢查請求是否是請求者或超級用戶,但無法實現此目的。從管理員驗證類中獲取用戶

我已經嘗試了「request.user.is_superuser」,「user.is_superuser」,「self.user.is_superuser」和「self.request.user.is_superuser」,但似乎沒有任何工作。

class ChannelStatusValidForm(forms.ModelForm): 
    class Meta: 
      model = ChannelStatus 
    def clean(self): 
     cleaned_data = self.cleaned_data 
     mydate = cleaned_data.get("date") 
     today = date.today() 
     if request.user.is_superuser:## here is the problem 
      return cleaned_data 
     elif (today - timedelta(days=2)) > mydate: 
      raise forms.ValidationError("Invalid date, maximum 2 days allowed.") 
     else: 
      return cleaned_data 
+0

這是因爲您沒有'request'對象。你需要給表單驗證函數一個'RequestContext'對象。 – 2009-09-07 09:22:07

+0

我嘗試過類ChannelStatusValidForm(forms.ModelForm,request):但它似乎不起作用。 – Hellnar 2009-09-07 09:34:35

+0

是的,我試過Google搜索,我找不到,或者找出如何發送它。我確信我已經看到它完成了。我相信有人會幫助你儘快解決問題! – 2009-09-07 09:44:00

回答

1

添加(及調整)丹尼爾·羅斯曼的回答從another question

class ChannelStatusValidForm(forms.ModelForm): 

    def __init__(self, *args, **kwargs): 
     self.request = kwargs.pop('request', None) 
     super(MyForm, self).__init__(*args, **kwargs) 


    def clean(self): 
     cleaned_data = self.cleaned_data 
     mydate = cleaned_data.get("date") 
     today = date.today() 
     if self.request.user.is_superuser: 
      return cleaned_data 
     elif (today - timedelta(days=2)) > mydate: 
      raise forms.ValidationError("Invalid date, maximum 2 days allowed.") 
     else: 
      return cleaned_data 

,並在您的視圖:

myform = ChannelStatusValidForm(request.POST, request=request) 
+0

太棒了!我無法得到最後一部分的觀點。對於管理員模式,我該如何添加此視圖?換句話說,我將在哪裏添加「myform = ChannelStatusValidForm(request.POST,request = request)」 – Hellnar 2009-09-07 12:10:16

+0

不確定 - 我無法立即在管理站點的文檔中看到任何內容(http:// docs.djangoproject。 com/en/dev/ref/contrib/admin /),但你可能比我更有運氣了。 – 2009-09-07 12:40:57

+1

您需要重寫ModelAdmin的'add_view'和/或'change_view'方法。在'django.contrib.admin.options'中查找現有版本。不幸的是,會有相當數量的複製粘貼代碼來更改表單實例化的行,但目前無法幫助。 – 2009-09-07 13:22:01

0

有一種方法來實現這一目標而不產生額外的管理意見:use a form metaclass in get_form()

class ChannelStatusValidForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs): 
     self.request = kwargs.pop('request', None) # Now you can access request anywhere in your form methods by using self.request.    
     super(ChannelStatusValidForm, self).__init__(*args, **kwargs) 

    def clean(self): 
     cleaned_data = self.cleaned_data 
     mydate = cleaned_data.get("date") 
     today = date.today() 
      request = self.request 
     if request.user.is_superuser: 
      return cleaned_data 
     elif (today - timedelta(days=2)) > mydate: 
      raise forms.ValidationError("Invalid date, maximum 2 days allowed.") 
     else: 
      return cleaned_data 
    class Meta: 
      model = ChannelStatus 

class ChannelStatusAdmin(admin.ModelAdmin): 
    form = ChannelStatusValidForm  
    def get_form(self, request, obj=None, **kwargs): 
     AdminForm = super(ChannelStatusAdmin, self).get_form(request, obj, **kwargs) 
     class ModelFormMetaClass(AdminForm): 
      def __new__(cls, *args, **kwargs): 
       kwargs['request'] = request 
       return AdminForm(*args, **kwargs) 
     return ModelFormMetaClass 
相關問題