简体   繁体   English

在 Django Rest 框架中复制 Model 字段实例列表

[英]Copying a list of Model Fields instances in Django Rest Framework

I find myself stuck with this problem and I might have phrased the title wrong but will explain what I want to do and what I am willing to accomplish.我发现自己被这个问题困住了,我可能用错了标题,但会解释我想做什么以及我愿意完成什么。 I have a model for attedance and on a daily basis a teacher has to mark an attendance Register.我有一个 model 用于出勤,并且每天都有老师必须标记出勤登记册。 I wouldnt want a situation where when generating a register data has to be captured on a daily basis but I would like were a table like form with a copy of all students in a class created with only status being the fields to be edited and saved, thus on save of each student their day attendance record is created.我不希望每天必须捕获生成注册数据时的情况,但我希望有一个表格,其中包含 class 中所有学生的副本,创建的只有状态是要编辑和保存的字段,因此,在保存每个学生时,他们的日出勤记录就会被创建。 My model is like as follows:我的 model 如下:

class StudentAttendanceTimeSheet(models.Model):
    DAILY_ATTENDANCE_STATUS_CHOICES = [(1, 'Present'), (2, 'absent'), (3, 'sick')]
   
   student = models.Foreignkey('Student', related_name='attendances', on_delete=models.CASCADE)
   attendance_status = models.CharField(max_length=50, choices=DAILY_ATTENDANCE_STATUS_CHOICES, default='1')
   date = models.DateField(default=datetime.today())
   class = models.ForeignKey('Class', related_name='class_timesheets, on_delete=models.CASCADE)
   

I would like to make a step form where by when I hit the select button a copy would be made with those fields prepopulated with database data for a specific class.我想制作一个步骤表单,当我点击 select 按钮时,将使用预先填充特定 class 的数据库数据的那些字段进行复制。 Thus a list of all students enrolled in that class.因此,所有注册该 class 的学生的列表。

May someone help me with how they would create a Django Rest Create View for such a problem.可能有人帮助我解决如何为此类问题创建 Django Rest 创建视图。 Thanks in advance.提前致谢。 Ngiyabonga恩吉亚邦加

This is how I handled this problem这就是我处理这个问题的方式

from django.db import models
from basedata.models import SoftDeletionModel
from basedata.constants import ATTENDANCE_STATUS_CHOICES
from django.conf import settings


    

class Attendance(SoftDeletionModel):
    date = models.DateTimeField(auto_now_add=True)
    klass = models.ForeignKey(
                        'klasses.StudentClass', 
                        models.SET_NULL, 
                        null=True,
                        blank=True,
                        related_name='attendances'
                    )
    recorded_by = models.ForeignKey(
                            "people.StaffUser", 
                            on_delete=models.SET_NULL, 
                            related_name='recorder', 
                            null=True
                        )
    complete=models.BooleanField(default=False, blank=True)

    
    def save(self, *args, **kwargs):
        super(Attendance, self).save(*args, **kwargs)
        if self.records.count() <= 0:
            for student in self.klass.students.all():
                self.records.create(student=student, status='present') 
        # separate sql query i guess for each entry created.

    def __str__(self):
        return f"{self.id}, {self.date}"




class AttendanceRecord(SoftDeletionModel):
    attendance = models.ForeignKey(
                'Attendance', 
                on_delete=models.SET_NULL, 
                null=True, 
                blank=True,
                related_name='records'
            )
    student = models.ForeignKey(
                        'people.Student', 
                        on_delete=models.SET_NULL,
                        blank=True,
                        null=True,
                        related_name='attendancerecords'

                    )
    status = models.CharField(max_length=100, choices=ATTENDANCE_STATUS_CHOICES)


    def __str__(self):
        return f"(STUDENT: {self.student}, STATUS: {self.status})"

By looping over all students in the class I can create default database entries for all students and then edit afterwards通过遍历 class 中的所有学生,我可以为所有学生创建默认数据库条目,然后进行编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM