Sasha
Sasha

Reputation: 367

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'

Upvotes: 1

Views: 1373

Answers (2)

Shark
Shark

Reputation: 267

Not only the '&' but also Comma ',' represents AND in django Q objects so you can also try:

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

Although I couldn't recreate the error that you get. Also please make sure slug,self.author and self.id have proper type of data to go into respective fields.

Here are the docs for complete reference

Upvotes: 0

alecxe
alecxe

Reputation: 473763

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()

Upvotes: 2

Related Questions