学院管理系统中如何设置所有学生呈现的初始值| Django 管理内联
我正在 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人评论的那样,将参数“default='True'”添加到模型字段即可解决问题。如果您发现您需要的不仅仅是“是或否”,但仍然想要默认值,您可以执行以下操作:
这将为您的字段创建一个下拉列表,并将默认设置作为列表中的第一个选择,在此案例“现在”。选择列表的每个元组中的第一个字符串将被保存到数据库中。第二个字符串对用户可见,因此如果有用例,您可以更改这些字符串。
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:
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.