For a user to be able to see only their items, these items must be marked with the user who actually owns them.
So, your model of ToDo
must also contain a foreign key field from the Django user table.
class Item(models.Model):
...
usuario = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
Settings.AUTH_USER_MODEL contains the table name that Django uses to store users.
Thus, when saving a Whole at the base, you must indicate which user owns this item. The logged-in user is inside the object request
, received as a parameter in the view:
def salvarItem(request):
item.usuario = request.user
item.save()
And, finally, when listing the only items of the logged-in user, just filter through this field:
def listarItems(request):
itens = Item.objects.filter(usuario=request.user)
return render('tela.html', { 'itens': itens})
Study on
request.user
– Paulo Marques