Search code examples
djangodjango-modelsdjango-querysetdjango-q

Django ~Q queries


Here is my error, and I really can't find anything similar to my problem:

from django.db.models import Q    
_entry = Entry.objects.get(Q(slug=slug, author=self.author) & ~Q(id=self.id))

TypeError: bad operand type for unary ~: 'Q'


Solution

  • An alternative to what you are trying to do with Qs, would be to use filter()+exclude()+get():

    _entry = Entry.objects.filter(slug=slug, author=self.author).exclude(id=self.id).get()