您好,我正在开发 Django Web 应用程序 - 我有一个选择框,其中包含特定模型中的所有对象。选择框是在模板中创建的,它不是直接表单的一部分。当我提交时,我能够访问用户选择的值,但是当我尝试使用该值作为查询集的一部分时,它不会返回预期值。
models.py
class TaskGroup(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
def __str__(self):
return self.name
class TaskGroupDetail(models.Model):
taskGroup = models.ForeignKey(TaskGroup, null=True, blank=True)
taskType = models.ForeignKey(TaskType, null=False, blank=False)
views.py
def 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)当我得到我期望的值时引用上面的代码,我可以看到该值与taskGroup我尝试在查询集中返回的对象的字段相匹配。我假设这与值从选择传递到视图的方式有关(就像它不是 TaskGroup 对象或其他东西),但我不太确定此时要尝试修复它。有什么想法吗?
守着星空守着你
相关分类