如何将值放在modelformset字段中?

时间:2019-06-28 06:46:35

标签: python django

我有一个供学生参加的modelformset。 models.py看起来像:

class Attendance(models.Model):
    Student = models.CharField(max_length=100, blank=False)
    Hour = models.CharField(max_length=1, blank=False)
    Subject = models.CharField(max_length=8, blank=False)
    Date = models.DateTimeField(default=timezone.now)
    Presence = models.BooleanField(default=False, blank=False)
    def __str__(self):
        return f'{self.Student}'

还有我的views.py

ef academics(request):
    if request.user.is_staff:
        formset = modelformset_factory(Attendance, fields=('__all__'), extra=60)
        if request.method == "POST":
            form=formset(request.POST)
            form.save()
        form = formset(queryset=Attendance.objects.none())
        return render(request, 'console/academics.html',{'form':form})
    else:
        context = {
            'attends': Attendance.objects.all().exclude(Date=timezone.now()),
            'todays': Attendance.objects.filter(Date=timezone.now())[:8]
        }
        return render(request, 'student/academics.html',context)

如何将“学生”字段值的值设置为User.objects.filter(profile__Year='SY',profile__Department='CSE')的名称,其中包含一个系和一年的学生的名称。

<form method="POST" action="{% url 'academics' %}" style=" padding: 5%">
    {% csrf_token %}
    {{form.as_p}}
<button type="Submit">Submit</button>

0 个答案:

没有答案