Nawal
Nawal

Reputation: 3

displaying a user tasks

model.py

class Person(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __unicode__(self):
        return self.user.username

class Task(models.Model):
    username = models.ForeignKey(User, on_delete=models.CASCADE)
    titre = models.CharField(max_length=100)
    date = models.DateField()
    objectif = models.CharField(max_length=1000)
    theme = models.CharField(max_length=20)

views.py

tasks = Task.objects.filter()
return render_to_response('home.html',{'tasks':tasks})

the problem is it display all the tasks in the table but i want to display just the tasks of the user who is logged in how can i do that

Upvotes: 0

Views: 42

Answers (3)

biniow
biniow

Reputation: 401

Task.objects.filter(username=request.user)

Upvotes: 0

Marcos Felipe
Marcos Felipe

Reputation: 118

You just need to filter by the user on request.user

tasks = Task.objects.filter(username=request.user)

Upvotes: 1

Serafeim
Serafeim

Reputation: 15104

Your view function (which you aren't fully displaying) has a request parameter. This parameter can be used to retrieve the current request's user. You should then use that to filter the tasks:

def the_view(request):    
    # .... 
    tasks = Task.objects.filter(username=request.user)
    return render_to_response('home.html',{'tasks':tasks})

Upvotes: 0

Related Questions