Question

I have a querylist that returns possible solutions to a problem. The list returns the results that I am expecting. I am trying to add a checkbox form that correlates to each item on the querylist. Its not too fancy, I just need to check the box and update the model. I have two models:

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      

Now in my View I have the following queryset:

solution_list = TaskSolution.objects.filter(relatedtask__id=task_id) 

This is fine as it returns the solutions as expected. The issue I am now having is I would Like the MarkSolved form initial value to correlate with the solution_list value.

 393 if request.method == 'POST':                                                                                                                                                                                                                              
 394         mark_solved_form = PostMarkedSolved(data=request.POST, instance=solution_task)

Essentially I am looking for a query like this:

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;

Where all the values match up from the two models in the queryset. That query returns the exact results I would like, but I am at a loss on now to populate the initial value based on the solution_list results.

Was it helpful?

Solution

I think the model MarkedSolved needs related field to TaskSolution model

replce

related_solution = models.IntegerField(editable=False)

with

related_solution = models.ForeignKeyField(TaskSolution)

Tempalte

   {% for item in solved_list%}
        <input type = "checkbox" name="{{item.id}}" > {{item.solution}}
   {% endfor %}

Views

 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
            )

I didnt tested the above code, but this will gives you the idea

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top