2011-09-14 4 views
0

일반 외래 키를 사용하여 인라인 관리 모델을 얻었으며 model clean() 메서드에서 해당 속성 집합의 유효성을 검사하려고합니다. 새 모델을 추가하면 clean() 메서드에서 content_type과 object_id가 설정되지 않지만 기존 모델을 변경하려고하면 content_type 속성에 액세스 할 수 있습니다.django 인라인 관리 모델 및 일반 관계

처음으로 새 모델을 추가 할 때 content_type을 가져 오는 방법이 있습니까?

모든 힌트 또는 URL을 알려 주시면 감사하겠습니다. 감사합니다. & & 좋은 하루 되세요! :)

Basti

답변

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