如何编写代码,以便在带有django的模型中只选择一个单选按钮。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何编写代码,以便在带有django的模型中只选择一个单选按钮。相关的知识,希望对你有一定的参考价值。

#models.py
from django.db import models
from django.contrib.auth.models import User

CHOICES = (('1','Earned Leave'),('2','Casual Leave'),('3','Sick Leave'),('4','Paid Leave'))

class Leave(models.Model):

    employee_ID = models.CharField(max_length = 20)

    name = models.CharField(max_length = 50)

    user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)

    department = models.CharField(max_length = 50)

    designation = models.CharField(max_length = 50)

    type_of_leave = models.CharField(max_length = 15, choices = CHOICES)

    from_date = models.DateField(help_text = 'mm/dd/yy')

    to_date = models.DateField(help_text = 'mm/dd/yy')

    reporting_manager = models.CharField(max_length = 50, default = None, help_text = '0001_manager, 0002_manager')

    reason = models.CharField(max_length= 180)

    accepted = models.BooleanField(('accept'), default= False)

    rejected = models.BooleanField(('reject'), default = False)

    reason_reject = models.CharField(('reason for rejection'),max_length=50) // this didn't help me.

    def __str__(self):
        return self.name

这是一个请假表单,其中只应选择两个字段(accepted, rejected),如果未选择字段拒绝字段,则reason_reject不应显示在/admin面板中。

#forms.py
from django import forms
from lrequests import models

class LeaveRequestForm(forms.ModelForm): 

    class Meta:
        fields = ("name", "employee_ID", "department", "designation", "type_of_leave", "from_date", "to_date", "reporting_manager", "reason")
        model = models.Leave

用户填写表单并提交。所以,现在管理员必须接受它或拒绝它。只有在选择reason_reject字段后,才会向管理员显示rejected字段。这一切都应该只发生在管理方面。

#admin.py
from django.contrib import admin
from . import models

@admin.register(models.Leave)
class LeaveAdmin(admin.ModelAdmin):
    list_display = ["name"]
    #"employee_ID", "department", "designation", "type_of_leave", "from_date", "to_date", "reporting_manager", "reason", "accepted", "rejected", "reason_reject"

    list_filter = ['department','type_of_leave']

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return (qs.filter(reporting_manager=request.user.username) or qs.filter(employee_ID=request.user.username))



 # def get_readonly_fields(self, request, obj=None):
# # the logged in user can be accessed through the request object 
#     if obj and request.user.is_staff:
#         readonly_fields = [f.name for f in self.opts.fields]
#         readonly_fields.remove('accepted')
#         readonly_fields.remove('rejected')
#     return readonly_fields
#the above chunk of get_readonly_fields was written so that the admin could only either 'accept' or 'reject' the form.

    def get_fields(self, request, obj=None):
         fields = [f.name for f in self.opts.fields]
         if obj and obj.rejected:
            fields.append('reason_reject')
         return fields

通过运行上面的代码我得到KeyError at /admin/lrequests/leave/15/change/

"Key 'id' not found in 'LeaveForm'. Choices are: accepted, department, designation, employee_ID, from_date, name, reason, reason_reject, rejected, reporting_manager, to_date, type_of_leave, user."

以上是关于如何编写代码,以便在带有django的模型中只选择一个单选按钮。的主要内容,如果未能解决你的问题,请参考以下文章

如何保存具有多个图像的 django 模型?

如何让用户在 Django 中只喜欢/不喜欢帖子一次?

Django,如何从模型表单的选择字段中删除空白选择?

如何在Django中只在ModelForms表中正确提交一个表单?

如何编写带有子查询的 Django 查询作为 WHERE 子句的一部分?

如何以特定的 django 形式执行此操作?