学院管理系统中如何设置所有学生呈现的初始值| Django 管理内联

发布于 2025-01-11 10:59:43 字数 1215 浏览 0 评论 0原文

我正在 django 中构建大学管理系统,包括测验、LMS 和出勤等功能。

我想在 django admin inline 中默认列出所有 status=True 的学生。因此,教师不必为每个学生勾选状态复选框,默认情况下每个学生都应该在场(status=True),教师只需取消选中缺席的状态。我用谷歌搜索了很多,但很难找到解决方案。这就是我想要的: 输入图片此处描述

正如您所看到的,所有学生都在场 Status=True,这应该是默认行为

我的模型:

class Attendance(models.Model):
    course = models.ForeignKey(settings.COURSE_MODEL, on_delete=models.CASCADE)
    date = models.DateField(default=datetime.now)


class AttendanceStatus(models.Model):
    attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE)
    student = models.ForeignKey(settings.STUDENT_MODEL, on_delete=models.CASCADE)
    status = models.BooleanField()

这是管理员。

class AttendanceStatusInline(admin.TabularInline):
    model = AttendanceStatus
    can_delete = False
    fields = [ 'student', 'status' ]
    autocomplete_fields = [ 'student' ]


@admin.register(Attendance)
class AttendanceAdmin(admin.ModelAdmin):
    list_display = [ 'course', 'date']
    autocomplete_fields = ['course']
    inlines = [ AttendanceStatusInline ]

提前致谢。

I am building College management system in django including features such as quiz, lms and attendance and so on.

I want list all students with status=True by default in django admin inline. So teacher dont have to hammer the status checkbox for every student, every student should be present(status=True)by default and teacher only have to uncheck the status for whom who are absent. I have googled a lot but having hard time figuring out the solution. This is what I want:
enter image description here

As you can see all the students are present Status=True, this should be the default behaviour

My model:

class Attendance(models.Model):
    course = models.ForeignKey(settings.COURSE_MODEL, on_delete=models.CASCADE)
    date = models.DateField(default=datetime.now)


class AttendanceStatus(models.Model):
    attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE)
    student = models.ForeignKey(settings.STUDENT_MODEL, on_delete=models.CASCADE)
    status = models.BooleanField()

And here is the admin.

class AttendanceStatusInline(admin.TabularInline):
    model = AttendanceStatus
    can_delete = False
    fields = [ 'student', 'status' ]
    autocomplete_fields = [ 'student' ]


@admin.register(Attendance)
class AttendanceAdmin(admin.ModelAdmin):
    list_display = [ 'course', 'date']
    autocomplete_fields = ['course']
    inlines = [ AttendanceStatusInline ]

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2025-01-18 10:59:43

正如其他人评论的那样,将参数“default='True'”添加到模型字段即可解决问题。如果您发现您需要的不仅仅是“是或否”,但仍然想要默认值,您可以执行以下操作:

ATTENDANCE_CHOICES = [
    ('Present', 'Present'),
    ('Absent', 'Absent'),
    ('Late', 'Late'),
    ('Excused Absence', 'Excused Absence'),
    ]

class AttendanceStatus(models.Model):
    attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE)
    student = models.ForeignKey(settings.STUDENT_MODEL, on_delete=models.CASCADE)
    status = models.CharField(max_length=100, choices=ATTENDANCE_CHOICES, default=ATTENDANCE_CHOICES[0][0])

这将为您的字段创建一个下拉列表,并将默认设置作为列表中的第一个选择,在此案例“现在”。选择列表的每个元组中的第一个字符串将被保存到数据库中。第二个字符串对用户可见,因此如果有用例,您可以更改这些字符串。

As others have commented, adding the argument "default='True'" to your model field will do the trick. In the case where you find you need more than a "yes or no" but still want a default, you could do something like:

ATTENDANCE_CHOICES = [
    ('Present', 'Present'),
    ('Absent', 'Absent'),
    ('Late', 'Late'),
    ('Excused Absence', 'Excused Absence'),
    ]

class AttendanceStatus(models.Model):
    attendance = models.ForeignKey(Attendance, on_delete=models.CASCADE)
    student = models.ForeignKey(settings.STUDENT_MODEL, on_delete=models.CASCADE)
    status = models.CharField(max_length=100, choices=ATTENDANCE_CHOICES, default=ATTENDANCE_CHOICES[0][0])

This will create a dropdown for your field with a default set as the first choice in the list, in this case 'Present'. The first string in each tuple of the choices list will be saved to the database. The second string is visible to the user, so you could change these if there is a use case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文