【问题标题】:Field Validation in Admin when field are dependent on other fields当字段依赖于其他字段时,管理员中的字段验证
【发布时间】:2012-04-14 17:29:45
【问题描述】:

当各个字段相互依赖时,如何在管理员中应用验证?

例如假设我有一个字段 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(Interview, InterviewAdmin)

在 admin 中,如果我选择 revisit 复选框并在字段 interview_type(这将显示一个下拉菜单中选择 None、Paired Visit、Time Series)如果用户从该下拉列表中选择了 None 然后按保存按钮它应该向我抛出一个错误,就像正常的空白 = False 显示,说“此字段是必需的”

如何在字段相互依赖的情况下进行这种验证?

请忽略任何语法错误。

谢谢

【问题讨论】:

    标签: django-admin


    【解决方案1】:

    我对 response_change 和覆盖 clean 方法感到困惑,最后这就是我所做的

    通过在admin.py 中创建模型表单来覆盖 clean 方法

    class  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
    
    
    class InterviewAdmin(admin.ModelAdmin):
    
        # call the form for Validation
        form = InterviewAdminForm
        #....and so on ....
    

    【讨论】:

    • 有人能告诉我如何在应用验证的字段上方显示错误消息。目前正在管理页面顶部显示错误消息。我想在该字段上方显示。在这种情况下,它应该在 interview_type 字段上方,就像默认管理员在该字段上方显示“此字段是必需的”一样。提前致谢
    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2014-12-01
    • 1970-01-01
    • 2013-12-26
    • 2016-05-27
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多