动态更新Django

时间:2018-08-11 11:02:10

标签: django django-models django-views

在会话中创建新的sesid时,我需要将所有课程添加到Registration模型中的sesid中。

我正在使用Django创建结果管理系统。 我想做的就是

  • 创建会话(完成)
  • 然后,课程模型中的所有课程将自动位于 会话模型(我不需要单独添加),然后显示一个 可以向添加的课程中添加批处理的页面。
  • 提交后,相应批次的所有学生将 添加到课程中,从而进行会话并重定向到某个地方,以使用户可以将每门课程分配给特定的老师
  • 每个学生都有几个分数字段,以供指定的老师覆盖
  • 从表中输入(最好作为导入的excel文件)后,将计算结果并将其保存在数据库中

到目前为止,我已经做到了:

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()

如何进行数据库的动态更新?

1 个答案:

答案 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())