4

This question might be weird, but i want to retrieve objects except the objects created by the login in user

Well i tried this code in my view

def getobjects(request):
    products=Products.objects.filter(user!= request.user)

But does not work.

In model

class Products(models.Model):
    name=models.Charfield()
    user=models.ForeignKey(User)

I will need help with the right query function to handle this. Thanks in advance

Underoos
  • 4,708
  • 8
  • 42
  • 85
john
  • 61
  • 4
  • 1
    Sounds like you are looking for [exclude](https://docs.djangoproject.com/en/2.2/topics/db/queries/#retrieving-specific-objects-with-filters) – Abdul Niyas P M May 16 '19 at 09:10

1 Answers1

5

You can exclude request user

products=Products.objects.exclude(user = request.user)
shafik
  • 6,098
  • 5
  • 32
  • 50