2011-09-14 77 views
0

我有一個內聯的管理模型,帶有一個通用的外鍵,我想驗證它在模型clean()方法中的一組屬性。如果我添加一個新模型,那麼在clean()方法中都不會設置content_type和object_id,但是如果我嘗試更改現有模型的話,我可以訪問content_type屬性。django內聯管理模型和一般關係

當我第一次添加新模型時,是否有任何解決方法可以獲取content_type?

任何暗示或URL表示讚賞 感謝& &有一個愉快的一天! :)

巴斯蒂

回答

0

如果我理解你,像下面應該工作:

class MyModelForm(forms.ModelForm): 
    class Meta: 
     model = MyModel 

    def clean(self): 
     content_type = self.cleaned_data.get('content_type') 
     object_id = self.cleaned_data.get('object_id') 

     if content_type and object_id: 
      obj = content_type.get_object_for_this_type(pk=object_id) 
      # Check whatever on the object here, and if there's a problem: 
      # raise forms.ValidationError('Something is not right') 

     return self.cleaned_data