2011-09-14 7 views
0

一般的な外部キーを持つインライン管理モデルがあり、モデルのclean()メソッドでそのプロパティのセットを検証したいと考えています。新しいモデルを追加すると、content_typeとobject_idのどちらもclean()メソッドで設定されませんが、既存のモデルを変更しようとすると、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 
関連する問題