0

我有一个查询列表,可以返回问题的可能解决方案。该列表返回我期望的结果。我正在尝试添加一个与查询列表中的每个项目相关的复选框表单。它不是太花哨,我只需要选中该框并更新模型。我有两个模型

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 结果填充初始值。

4

1 回答 1

2

我认为 MarkedSolved 模型需要与 TaskSolution 模型相关的字段

重新

related_solution = models.IntegerField(editable=False)

related_solution = models.ForeignKeyField(TaskSolution)

模板

   {% 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
            )

我没有测试上面的代码,但这会给你的想法

于 2012-11-05T08:11:43.840 回答