john = Author.objects.get(pk=1)
paul = Author.objects.get(pk=3)
Entry.objects.filter(authors=[john, paul])
TypeError: Field ‘id’ expected a number but got [<Author: gamal>, <Author: kamel>].
See the docs for in.
i want to retrieve all entries that the two author [gamal, kamel] are participating in together not the entries everyone participate in ,
i wish you understand me sir.
What do your Entry
and Author
models look like?
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
def __str__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateField()
mod_date = models.DateField(default=date.today)
authors = models.ManyToManyField(Author)
number_of_comments = models.IntegerField(default=0)
number_of_pingbacks = models.IntegerField(default=0)
rating = models.IntegerField(default=5)
def __str__(self):
return self.headline
author has a many-to-many relation with entry
You’re going to want to write this as two separate conditions in your filter
. A filter adds an entry to a resultset only if all supplied conditions are True. (A logical AND condition)
For example, the docs for week
has the sample:
Entry.objects.filter(pub_date__week__gte=32, pub_date__week__lte=38)
which means that in their example, a row would be included in the results only if a row was on or between weeks 32 and 38.
i resolved it by 2 filter chains
Entry.objects.filter(authors=paul).filter(authors=john)
Thank you sir