您好,我正在開發(fā) Django Web 應(yīng)用程序 - 我有一個(gè)選擇框,其中包含特定模型中的所有對象。選擇框是在模板中創(chuàng)建的,它不是直接表單的一部分。當(dāng)我提交時(shí),我能夠訪問用戶選擇的值,但是當(dāng)我嘗試使用該值作為查詢集的一部分時(shí),它不會返回預(yù)期值。models.pyclass TaskGroup(models.Model): name = models.CharField(max_length=100, null=False, blank=False) def __str__(self): return self.nameclass TaskGroupDetail(models.Model): taskGroup = models.ForeignKey(TaskGroup, null=True, blank=True) taskType = models.ForeignKey(TaskType, null=False, blank=False)views.pydef CreateShipmentView(request): if request.method == 'POST': shipment_form = CreateShipmentForm(request.POST) if shipment_form.is_valid(): new_shipment = shipment_form.save() group = request.POST.get('groups') #get the task group selected print(group) #this returns the expected value ..... #if a task group was added, create the tasks groupDetails = TaskGroupDetail.objects.get(taskGroup = group) #this returns a blank queryset print(groupDetails) #this returns a blank queryset return redirect('ShipmentListView') ... else: shipmentForm = CreateShipmentForm() groups = TaskGroup.objects.all() context = { 'shipmentForm': shipmentForm, 'groups': groups, } return render(request, 'create-shipment.html', context)print(group)當(dāng)我得到我期望的值時(shí)引用上面的代碼,我可以看到該值與taskGroup我嘗試在查詢集中返回的對象的字段相匹配。我假設(shè)這與值從選擇傳遞到視圖的方式有關(guān)(就像它不是 TaskGroup 對象或其他東西),但我不太確定此時(shí)要嘗試修復(fù)它。有什么想法嗎?
如何將選擇框的值作為外鍵傳遞(Django)
函數(shù)式編程
2024-01-24 20:49:57