在会话中创建新的sesid时,我需要将所有课程添加到Registration模型中的sesid中。
我正在使用Django创建结果管理系统。 我想做的就是
到目前为止,我已经做到了:
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
class Course(models.Model):
cid = models.AutoField(primary_key=True)
cnam = models.CharField(max_length=200)
cidn = models.IntegerField()
cred = models.IntegerField()
def __str__(self):
return 'IT-' + str(self.cidn) + ' - ' + self.cnam
class Student(models.Model):
snam = models.CharField(max_length=200)
sid = models.AutoField(primary_key=True)
sroll = models.IntegerField()
sreg = models.IntegerField()
sbtc = models.IntegerField()
sses = models.CharField(max_length=10)
def __str__(self):
return self.snam
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
full_name = models.CharField(max_length=200, null=True)
umob = models.CharField(max_length=20, blank=True, default='')
ulogo = models.ImageField(upload_to='media', blank=True)
def __str__(self):
return self.user.username
def createprofile(selfsender, **kwargs):
if kwargs['created']:
user_profile = Teacher.objects.creeate(user=kwargs['instance'])
class Session(models.Model):
sesid = models.IntegerField(primary_key=True,verbose_name= ('Session'))
def __str__(self):
return str(self.sesid)
def get_absolute_url(selfself):
return reverse('Dashboard:session')
class Registration(models.Model):
session = models.ForeignKey(Session, on_delete=models.CASCADE)
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
# def __str__(self):
# return str(self.session.sesid) + ' - ' + 'IT-' + self.str(course.cidn) + ' - ' + self.course.cnam + ' - ' + self.str(Teacher.user)
class Result(models.Model):
reg = models.ForeignKey(Registration, on_delete=models.CASCADE)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
ct1 = models.FloatField(null=True, blank=True)
ct2 = models.FloatField(null=True, blank=True)
ct3 = models.FloatField(null=True, blank=True)
asn = models.FloatField(null=True, blank=True)
# avg
atd = models.IntegerField(null=True, blank=True)
#total
def __str__(self):
return str(self.reg.session) + ' - ' + 'IT-' + str(self.reg.course.cidn) + ' - ' + self.student.snam
views.py:
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView
from django.shortcuts import render, redirect
from django_tables2 import RequestConfig
from .tables import *
from .models import *
from .forms import CustomUserChangeForm
class Login(generic.CreateView):
form_class = CustomUserChangeForm
success_url = reverse_lazy('index')
template_name = 'Dashboard/login.html'
class IndexView(ListView):
template_name = 'Dashboard/index.html'
def get_queryset(self):
return Course.objects.all()
def course(request):
table = CourseTable(Course.objects.all())
RequestConfig(request).configure(table)
return render(request, 'Dashboard/course.html', {'table': table})
def teacher(request):
table = TeacherTable(Teacher.objects.all())
RequestConfig(request).configure(table)
return render(request, 'Dashboard/teacher.html', {'table' : table})
def student(request):
table = StudentTable(Student.objects.all())
RequestConfig(request).configure(table)
return render(request, 'Dashboard/student.html', {'table' : table})
def result(request):
table = ResultTable(Result.objects.all())
RequestConfig(request).configure(table)
return render(request, 'Dashboard/result.html', {'table' : table})
class SessionView(CreateView,ListView):
template_name = 'Dashboard/createSession.html'
model = Session
fields = ['sesid']
def get_queryset(self):
return Session.objects.all()
如何进行数据库的动态更新?
答案 0 :(得分:0)
根据评论中的解释,类似这样的事情应该可以解决。
为了实现可重用性,您还可以将for course...
位移到Session
上的某个方法上,例如add_courses(self, teacher): ...
class SessionView(CreateView, ListView):
template_name = 'Dashboard/createSession.html'
model = Session
queryset = Session.objects.all()
fields = ['sesid']
def form_valid(self, form): # this will be the creation form
instance = form.save() # save the empty session
for course in Course.objects.all():
Registration.objects.create(
session=instance,
course=course,
teacher=self.request.user.teacher, # (Is this correct?)
)
return HttpResponseRedirect(self.get_success_url())