I’ve a problem and i don’t know the right way to solve it.
Basically,i’ve a mode, called Task:
class Task(models.Model):
#mto1: many task made of one template
template = models.ForeignKey(Template)
STATUS_CHOISE = (('PR', 'In process'), ('ST', 'Stopped'), ('FN', 'Finished'), ('DL', 'Deleted'),)
status = models.CharField(max_length=2, choices=STATUS_CHOISE, default='ST')
responses_required = models.IntegerField(default=0)
date_deadline = models.DateTimeField(auto_now=False, auto_now_add=False)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
which can have several answers
class Response(models.Model):
#mto1: many Responses generated for one task
task = models.ForeignKey(Template)
STATUS_CHOISE = (('PR', 'Process'), ('FN', 'Finished'))
status = models.CharField(max_length=2, choices=STATUS_CHOISE, default='ST')
date_finished= models.DateTimeField(auto_now_add=False, auto_now=True)
date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
ip_address=models.IPAddressField()
now, i would like to display all the task of one user (which comes from the template model, and this is done) with additional information about the answers, which are
– number of answer give (so which status is = FN)
– number of total answer (status FN or PR)
– and maybe some extra info.
i did this
@login_required
def TemplateList(request):
task_list = Task.objects.filter(user=request.user)
return render_to_response('task_list.html',{'task_list':task_list}, context_instance=RequestContext(request))
but this just display the data present in the model, what about the value i’ve to compute?
so far i did it with CustomTag, but it doesn’t seem a clean solution. i would rather do the logic in the view and then use the template just for display results.
But: how can i add this value to each item of the Task List?
do i have to do like in this: Passing additional data to a template in Django so create the objects from scratch (which requires me some logic in the template as well since i’ve to match the value in the list with the objects i crate)? or is there any better solution?
i tried generic view ,like list_detail.object_list
in this way
def TaskListDetail(request):
return list_detail.object_list(
request,
queryset = Task.objects.filter(user=request.user),
template_name = 'task_list.html',
template_object_name = 'task_list',
)
but first, it does not show anything (while the other view shows data), second i don’t know how to add extra data in a way that they are matched with the item of the list.
any suggestion?
You can add a method to your model.
In your template, you can access this with: