【问题标题】:Django Admin Validation for Inline ModelAdmin内联 ModelAdmin 的 Django 管理员验证
【发布时间】:2019-12-23 12:11:35
【问题描述】:

我正在为我的管理页面使用表格内联,其中一条指令可能有多个 scrap_code 和 event_code。但是一个 scrap_code 或 event_code 不能有多个指令。 我已经在整个表格中制作了 scrap_code 和 event_code,这样它们就不会被复制。

我的管理员.py

class InstructionAdmin(admin.ModelAdmin):
    inlines = [ ScrapEventInstructionMapInline, ]
    fields=('name',)
    form = InstructionMapForm

当用户尝试输入已经存在的 event_code 或 scrap_code 时,我需要向他们显示警报。 但问题是即使我们有数据指令,s_code 和 e_code 也以 None 的形式出现

我的 forms.py 文件:-

class InstructionMapForm(forms.ModelForm):

    def clean(self):
        instruction = self.cleaned_data.get('instruction')
        s_code      = self.cleaned_data.get('scrap_code')
        e_code      = self.cleaned_data.get('event_code')

        qs = ScrapEventInstructionMap.objects.all()
        if s_code:
            dup_scrap = list(ScrapEventInstructionMap.objects.filter(scrap_code=s_code).values('scrap_code'))
            if dup_scrap:
                raise forms.ValidationError ('The Scrap Code provided ({}) already exists, kindly edit it or provide another Scrap Code'.format(s_code))

        elif e_code:
            dup_event = list(ScrapEventInstructionMap.objects.filter(event_code=e_code).values('event_code'))
            if dup_event:
                raise forms.ValidationError ('The Event Code provided ({}) already exists, kindly edit it or provide another Event Code'.format(e_code))

如何获取避免无的数据?以及如何向用户显示警报?

【问题讨论】:

  • 不要在查询集上直接调用list(),它已经是list了。
  • 知道了,但是指令、s_code 和 e_ode 都没有。
  • 首先,e_code 和 s_code 都是 None。其次,它不会进入 if 条件。当我输入相同的 event_code 或 scrap_code 时,它​​应该进入循环并引发 ValidationError 如果它已经存在

标签: python django django-forms django-admin


【解决方案1】:

类 ScrapEventInstructionAdminFormset(forms.models.BaseInlineFormSet):

def clean(self):
    forms = [form for form in self.forms if not form.cleaned_data.get('DELETE')]
    for form in forms:
        scrap_code = form.cleaned_data.get('scrap_code')
        event_code = form.cleaned_data.get('event_code')
        if form.instance.id:
            scp = SCPInstruction.objects.exclude(id=form.instance.id)
            is_scrap_code_exists = scp.filter(instruction=form.instance.instruction, scrap_code=scrap_code)
            is_event_code_exists = scp.filter(instruction=form.instance.instruction, event_code=event_code)
        else:
            is_scrap_code_exists = SCPInstruction.objects.filter(instruction=form.instance.instruction, scrap_code=scrap_code)
            is_event_code_exists = SCPInstruction.objects.filter(instruction=form.instance.instruction, event_code=event_code)

        if is_scrap_code_exists:
            form.add_error('scrap_code', 'The Scrap Code provided ({}) already exists, kindly edit it or provide another Scrap Code'.format(scrap_code))
        if is_event_code_exists:
            form.add_error('event_code', 'The Event Code provided ({}) already exists, kindly edit it or provide another Event Code'.format(event_code))

    return self.forms

【讨论】:

  • 对我不起作用。再次说明,e_code 和 s_code 将作为 None 出现。如果我正在打印
  • 首先,e_code 和 s_code 都是 None。其次,它不会进入 if 条件。当我输入相同的 event_code 或 scrap_code 时,它​​应该进入循环并引发 ValidationError 如果它已经存在
  • InstructionMap 模型中 ScrapEventInstructionMap 的属性名称是什么。
  • 其实我已经为指令模型使用了代理模型继承。然后 ScrapEventInstructionMap 带有指令的 ForeignKey 到带有 scrap_code 和 event_code 的指令表。对于管理员,我在使用 TabularInline 的地方使用了 InlineModelAdmin。
  • 是scrap_code和event_code是InstructionMap的列吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2011-06-02
  • 1970-01-01
  • 2017-06-28
  • 2011-04-11
相关资源
最近更新 更多