从外部模型中输入可选项

时间:2014-04-29 08:07:30

标签: django django-models django-views

我有 models.py

class trainee(models.Model):
        username = models.TextField(verbose_name = 'username')

class learningObjective(models.Model):
        trainee = models.ForeignKey(trainee, verbose_name = "Azubi")
        learningObjectives = models.TextField(verbose_name = 'Lernziel')

class completedTask(models.Model):
        trainee = models.ForeignKey(trainee, verbose_name = "Azubi")
        learningObjectivecompletedTask = models.ManyToManyField(learningObjective, verbose_name = "Lernziel")
        date = models.DateField(verbose_name = 'Datum')
        duration = models.IntegerField(verbose_name = 'Dauer')
        documentationReference = models.TextField(verbose_name = 'Dokumentationsverweis', blank = True)
        educationDepartment = models.TextField(verbose_name = 'Ausbildungsabteilung')
        completedtasks = models.TextField(verbose_name = 'Ausgefuehrte Taetigkeiten')

这是 views.py

的一部分
@login_required(login_url='login')
def create_at(request):
        trainee_objects = trainee.objects.all()
        usernameID = 1
        for traineeUser in trainee_objects:
                if traineeUser.username == request.user.username:
                        usernameID = traineeUser.id
        if request.method == 'POST':
                form = completedTaskForm(request.POST)
                if form.is_valid():
                        neueAT=completedTask(date=request.POST['Datum'], duration=request.POST['Dauer'], educationDepartment=request.POST['Abteilung'], completedtasks=request.POST['AT'], trainee_id=usernameID)
                        neueAT.save()
                        for ct in learningObjective.objects.filter(learningObjectives=request.POST.get['Lernziel']):
                                 neueAT.learningObjectivecompletedTask.add(ct)
                 else:
                        form = completedTask()
         return render(request, 'at.html', {'at': completedTask.objects.all(), 'Lernziel': learningObjective.objects.all()})

所以,我现在有了一个字段,我必须填写模型 completedTask 。在有人没有填写实际需要的数据之前,一切正常。我正在编写一个APP来记录您在生产时间内所做的工作,当然您应该立即正确地填写所有条目。但如果不能立即制作一个条目,则不应该抛出错误。如何使多个字段可选。我认为我只是可以创建一个默认值,当用户更新其条目时,该值将被删除。但我不想这样做。在我的数据库中有一个空白条目会很酷,现在就是这种情况。但是,当我这样做时,它会给我以下错误:

  

/ programm / create_at

中的MultiValueDictKeyError

它创建条目和所有内容,这一切都很好,但我想摆脱错误。是否有模板标签,可以检查字段是否有输入?我尝试搜索它没有任何成功。如果您需要更多信息,请告诉我。

编辑(这些是我的表单,第一次使用它们)

from django import forms
from programm.models import trainee, todoList, learningObjective, topic, person, completedTask, remark

class completedTaskForm(forms.Form):
        trainee = forms.IntegerField(required = True)
        learningObjectivecompletedTask = forms.CharField(required = False)
        date = forms.DateField(required = True)
        duration = forms.IntegerField(required = True)
        documentationReference = forms.CharField(required = False)
        educationDepartment = forms.CharField(required = True)
        completedtasks = forms.CharField(required = True)

不再有任何条目。 print(form.error)的输出如下:

    
  • date
  • 这个   字段是必填字段。
  • 持续时间
  • 此字段是必填字段。
  •   
  • completedtasks
  • 此字段为   
  • educationDepartment
  • 此字段是必填字段。
  •   

    请指导我,此刻我已经陷入困境。

    1 个答案:

    答案 0 :(得分:0)

    Woo我为自己感到骄傲:P解决方案是 Forms

    from django import forms
    from programm.models import trainee, todoList, learningObjective, topic, person, completedTask, remark
    
    class completedTaskForm(forms.Form):
            trainee = forms.IntegerField(required = True)
            learningObjectivecompletedTask = forms.CharField(required = False)
            date = forms.DateField(required = True)
            duration = forms.IntegerField(required = True)
            documentationReference = forms.CharField(required = False)
            educationDepartment = forms.CharField(required = True)
            completedtasks = forms.CharField(required = True)
    

    我的观看

    中的此修改
    trainee_objects = trainee.objects.all()
            usernameID = 1
            for traineeUser in trainee_objects:
                    if traineeUser.username == request.user.username:
                            usernameID = traineeUser.id
    
            if request.method == 'POST':
                    forms = completedTaskForm(request.POST)
                    if forms.is_valid():
                            formlearningObjectives = forms.cleaned_data['formlearningObjectives']
                            formdate = forms.cleaned_data['formdate']
                            formduration = forms.cleaned_data['formduration']
                            formeducationDepartment = forms.cleaned_data['formeducationDepartment']
                            formcompletedtasks = forms.cleaned_data['formcompletedtasks']
    
                            neueAT=completedTask(date=formdate, duration=formduration, educationDepartment=formeducationDepartment, completedtasks=formcompletedtasks, trainee_id=usernameID)
                            neueAT.save()
                            for ct in learningObjective.objects.filter(learningObjectives=formlearningObjectives):
                                    neueAT.learningObjectivecompletedTask.add(ct)
                    else:
                            form = completedTask()
                            print("Es hat fehlgeschlagen!" + formset.errors)
            return render(request, 'at.html', {'at': completedTask.objects.all(), 'Lernziel': learningObjective.objects.all()})