如何使必填字段以Django形式对于超级用户是可选的?

时间:2019-03-11 21:45:23

标签: python django python-3.x postgresql

我正在尝试将必填表单字段设置为项目中超级用户的可选字段。 但是我被卡住了,因为我不知道该怎么做。当超级用户单击“ is_super”必填字段应变为可选时,我想将“ is_super”布尔值填入必填字段。

谢谢

这是表单字段的ScreenShot: enter image description here

这是我的form.py代码

class TrackerForm(forms.ModelForm):

    is_super = forms.BooleanField()
    class Meta:
        model = Tracker
        fields = ('is_super','cascade','Date','Technology', 'Type', 'Bandwidth_Checked_From_LSM', 'Market', 'eNB', 'LSM', 'CSMS', 'FE_Name', 'Mode_of_Communication', 'Activity', 'Activity_status', 'Site_Status_pre_Activity', 'Site_Status_post_Activity','E_Link_Status_of_BH0_for_CDU30','MJ_Object_Marked','RET','Alarms_Preventing_RET_Config','Frequency_Earfcn_Checked_from_LSM_BSM','IP_Route_or_IP_Address','Volte_MME_IP_Config','Review_LATP_Complete','Remarks','OAR_Date','OAC_Date','Lock_Unlock_Verified_By','Verify_Status','Final_Comments')

`

View.py:

@login_required
def tracker_new(request):
    if request.method == "POST":
        form = TrackerForm(request.POST)

        if form.is_valid():
            tracker = form.save(commit=False)
            tracker.admin = request.user
            tracker.created_date = timezone.now()
            tracker.save()
            return redirect('tracker_detail', pk=tracker.pk)
    else:
        form = TrackerForm
        return render(request, 'tracker/tracker_edit.html', {'form': form})

Model.py:

admin = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default='')

        cascade = models.CharField(max_length=255, default='')
        Technology_CHOICES1 = (
                ('800', '800'),
                ('1900', '1900'),

        )

        Technology = models.CharField(max_length=255, choices=Technology_CHOICES1)

        Type_CHOICES8 = (
                ('CDU10', 'CDU10'),
                ('CDU20', 'CDU20'),
                ('CDU30', 'CDU30'),
                ('NA', 'NA'),
        )

        Type = models.CharField(max_length=255, choices=Type_CHOICES8)

        Bandwidth_Checked_From_LSM_CHOICES10 = (
                ('3', '3'),
                ('5', '5'),
                ('10', '10'),
                ('NA', 'NA'),
        )

        Bandwidth_Checked_From_LSM = models.CharField(max_length=255, choices=Bandwidth_Checked_From_LSM_CHOICES10)

        market = (
                ('Kansas', 'Kansas'),
                ('Alaska', 'Alaska'),
                ('PR / VI', 'PR / VI'),


        )

        Market = models.CharField(max_length=255, choices=market)


        eNB = models.CharField(max_length=255, default='')
        LSM = models.CharField(max_length=255, default='')
        CSMS = models.CharField(max_length=255, default='')
        FE_Name = models.CharField(max_length=255, default='')


        Mode_of_Communication_CHOICES6 = (
                ('Whatsapp', 'Whatsapp'),
                ('Primary Bridge', 'Primary Bridge'),
                ('Secondary Bridge', 'Secondary Bridge'),
                ('IM', 'IM'),
                ('E-Mail', 'E-Mail'),
        )

        Mode_of_Communication = models.CharField(max_length=255, choices=Mode_of_Communication_CHOICES6)


        Activity_CHOICES2 = (
                ('C&I', 'C&I'),
                ('Troubleshoot', 'Troubleshoot'),
                ('LATP Testing', 'LATP Testing'),

        )

        Activity = models.CharField(max_length=255, choices=Activity_CHOICES2)

        Activity_status_CHOICES3 = (
                ('Open', 'Open'),
                ('Close', 'Close'),
                ('Handover', 'Handover'),
        )

        Activity_status = models.CharField(max_length=255, choices=Activity_status_CHOICES3,)

1 个答案:

答案 0 :(得分:1)

我建议根据已登录的用户状态(无论他/她是否为超级用户)返回不同的表格。 您可能还希望在后端(即在tracker_new中)以及在前端中实施适当的条件。

编辑:您也可以将管理门户用于特殊表单