我有一个querylist,可以返回问题的可能解决方案。该列表返回我期望的结果。我试图添加一个与查询列表中的每个项目相关的复选框表单。它不是太花哨,我只需要检查框并更新模型。我有两个模型:
149 class TaskSolution(models.Model):
150 solution = models.TextField()
151 submitter = models.ForeignKey(User, null=True, blank=True)
152 relatedtask = models.ForeignKey(Task, null=True, blank=True)
153 solutionnumber = models.IntegerField(editable=False)
154 date_created = models.DateTimeField(editable=False)
155 date_updated = models.DateTimeField(editable=False)
156 confimed_solution = models.BooleanField()
157 objects = SolutionVoteManager()
160 def save(self, *args, **kwargs):
161 if not self.id:
162 self.date_created = datetime.now()
163 self.date_updated = datetime.now()
164 super(TaskSolution, self).save(*args, **kwargs)
166 def __unicode__(self):
167 return self.id
169 def __unicode__(self):
170 return "%s" % self.object_pk
172 def __unicode__(self):
173 return self.solution
184 class MarkedSolved(models.Model):
185 is_solution = models.BooleanField(verbose_name='')
186 related_idea = models.ForeignKey(Idea, editable=False)
187 related_task = models.ForeignKey(Task, editable=False)
188 related_solution = models.IntegerField(editable=False)
189 date_updated = models.DateTimeField(editable=False)
191 def save(self, *args, **kwargs):
192 self.date_updated = datetime.now()
193 super(MarkedSolved, self).save(*args, **kwargs)
195 def __unicode__(self):
196 return self.id
198 def __unicode__(self):
199 return "%s" % self.object_pk
201 def __unicode__(self):
202 return "%s" % self.is_solution
204 class MarkedSolved(ModelForm):
205 class Meta:
206 model = MarkedSolved
现在,在我的查看中,我有以下查询集:
solution_list = TaskSolution.objects.filter(relatedtask__id=task_id)
这很好,因为它按预期返回解决方案。我现在遇到的问题是我希望MarkSolved表单初始值与solution_list值相关联。
393 if request.method == 'POST':
394 mark_solved_form = PostMarkedSolved(data=request.POST, instance=solution_task)
基本上我正在寻找这样的查询:
select * from markedsolved a, tasksolution b where a.related_solution=b.solutionnumber and a.related_solution=1 and b.solutionnumber=1 and a.related_task_id = 5 and b.relatedtask_id=5;
其中所有值与查询集中的两个模型匹配。该查询返回了我想要的确切结果,但我现在无法根据solution_list结果填充初始值。
答案 0 :(得分:2)
我认为MarkedSolved模型需要与TaskSolution模型相关的字段
replce
related_solution = models.IntegerField(editable = False)
与
related_solution = models.ForeignKeyField(TaskSolution)
<强> Tempalte 强>
{% for item in solved_list%}
<input type = "checkbox" name="{{item.id}}" > {{item.solution}}
{% endfor %}
<强>视图强>
if request.method == 'POST':
for soln in solutions_list:
solution_id = request.POST.get(str(soln.id))
if solution_id:
MarkedSolved.objects.create(
is_solution = True,
related_task = Task.objects.get(id=task_id),
related_solution = soln
)
我没有测试过上面的代码,但这会给你一个想法