Home > Back-end >  how to show only the data of the user that is logged in (Django)
how to show only the data of the user that is logged in (Django)

Time:02-23

So it shows data like all objects but now I only want to show the specific data of each user this is my views.py:

def Campos(request):
    dados = Cultivo.objects.all().order_by('name')    
    return render(request,'Agrefarm/Campos.html', {'dados':dados})

and this is my models.py

class Cultivo(models.Model):
    Dono = models.ForeignKey(User,default=None, on_delete=models.CASCADE)
    name = models.CharField(max_length=20)
    descrição = models.CharField(max_length=20)
    Arvores = models.IntegerField(default=0)

def __str__(self):
    return self.name

def curto(self):
    return self.descrição[:15]

CodePudding user response:

I believe you'll want to do something like this:

dados = Cultivo.objects.filter(Dono = request.user)

I believe that'll work for you.

  • Related