2012-03-31 7 views
0

お互いに依存している場合、さまざまなフィールドの管理者で検証を適用するにはどうすればよいですか?フィールドが他のフィールドに依存している場合の管理におけるフィールド検証

私はフィールドA(BooleanField)とフィールドB(CharField)を持っているとしましょう、管理者のユーザがフィールドA(チェックボックス)を選択し、フィールドBに何も入力しない場合、 と彼が保存しようとすると、通常の空白= Falseのようなエラーが発生します。だから私はどのように管理者のこの種の検証を行うことができます。

例:ユースケース

私は次の表の構造を有する持っている: -

INTERVIEW_TYPES =(

('default', 'None'), 
    ('Paired Visit','Paired Visit'), 
    ('Time Series', 'Time Series'), 

), 

クラスインタビュー(models.Model):

ic_number    = models.CharField(verbose_name ="Visit Configuration Number",max_length=20,unique=True,null =True,blank=True) 
ic_description   = models.TextField(verbose_name ="Visit Configuration Description",null = True,blank=True) 
title     = models.CharField(verbose_name ="Visit Configuration Title",max_length=80,unique=True) 
starting_section  = models.ForeignKey(Section) 
interview_type   = models.CharField(verbose_name = "Mapped Visit",choices=CHOICES.INTERVIEW_TYPES, max_length=80, default="Time Series") 
select_rating   = models.CharField(choices=CHOICES.QUESTION_RATING, max_length=80, default="Select Rating") 
view_notes    = models.CharField(choices=CHOICES.VIEW_NOTES, max_length=80, default="Display Notes") 
revisit    = models.BooleanField(default=False) 

を.....など......

class Meta: 
    verbose_name = 'Visit Configuration' 
    verbose_name_plural = 'Visit Configurations' 
    # ordering = ('rpn_number',) 

def __unicode__(self): 
    return self.title 

そのadmin.py

クラスInterviewAdmin(admin.ModelAdmin):

list_display = ('id','title', 'starting_section','ic_number','show_prior_responses') 
raw_id_fields = ('starting_section',) 

admin.site.register(インタビュー、InterviewAdmin)管理者で

、私は、チェックボックスを選択した場合ユーザがそのドロップダウンからNoneを選択してからSaveボタンを押すと、通常の空白= Falseのようなエラーが出るはずですが、これは再訪のフィールドとinterview_type(None、Paired Visit、Time Seriesの選択肢を持つドロップダウンを表示します) 、 "T彼のフィールドは必須です "

フィールドが互いに依存するこの種の検証はどのようにして行うことができますか?

「無視する構文エラーはありません。

おかげ

答えて

0

私はresponse_changeで混乱しましたし、きれいなメソッドをオーバーライドついにこれは私がadmin.py

クラスInterviewAdminForm(forms.ModelFormのモデルフォームを作ることで

オーバーライドクリーンな方法でやったことあります):

class Meta: 
    model = Interview 

def clean(self, *args, **kwargs): 
    cleaned_data = super(InterviewAdminForm, self).clean(*args, **kwargs) 

    if self.cleaned_data['interview_type'] == "default" \ 
    and self.cleaned_data['Revisit'] == True: 
     raise forms.ValidationError({'interview_type': ["error message",]}) 
    return cleaned_data 

クラスInterviewAdmin(admin.ModelAdmin):

# call the form for Validation 
form = InterviewAdminForm 
....and so on .... 

+0

妥当性検査が適用されたフィールドの真上にエラーメッセージを表示する方法を教えてもらえますか?現在、管理ページの上部にエラーメッセージが表示されています。私はそのフィールドの真上に表示したい。この場合、デフォルトの管理者がそのフィールドの真上にある「このフィールドは必須です」のように、interview_typeフィールドの上になければなりません。前もって感謝します –

関連する問題