2011-09-14 55 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