Hi all
I have a model called Task and a model called User. Between them is a many to
many relationship to simulate assigning tasks to users.
Basically I want to list all tasks in an HTML page. Each task will then have a
set of checkboxes representing every user, if that particular user is already
linked to that particular task then the checkbox should be ticked.
I have managed to do this, but using a really ugly hack, and I'm hoping for some
advice on how to better go about it. What I did was create a template filter
called is_assigned() using this code:
@register.filter
def is_assigned( user_id, task_id ):
"""We want to find out whether the given user ID is assigned to the given
task."""
task = Task.objects.filter( id = task_id ).get()
users = task.users.values( 'id' )
ids = []
for i in users: ids.append( i['id'] )
if user_id in ids: return True
else: return False
The view passes the template variables containing all users called all_users,
and all tasks called all_tasks. In the template I have the following code that
will set the checkbox to checked depending on whether the user is linked to the
task:
{% for task in all_tasks %}
{% for user in all_users %}
<input type="checkbox" name="users" value="{{user.id}}"
id="user{{user.id}}"\
{% if user.id|is_assigned:task.id%}checked{%endif%} />
{% endfor %}
{% endfor %}
How might I refactor all this code to make it more efficient and generally
better?
Thanks
Gabriel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---