Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I'm building a news website. While I tried to get the list of relative news which have the same tags. The error said:
The QuerySet value for an exact lookup must be limited to one result using slicing.
I have two models
News
and
Tag
. Tag is a many-to-many foreign key of
News
.
# "models.py"
class Tag(models.Model):
name = models.CharField(max_length=40)
class News(models.Model):
tag = models.ManyToManyField(Tag, blank=True, verbose_name='tag')
View:
def newsDetailView(request, news_pk):
news = get_object_or_404(News, id=news_pk)
tags = news.tag.annotate(news_count=Count('news'))
relative_news = News.objects.filter(tag=tags)
return render(request, "news_detail.html", {
'news': news,
'tags': tags,
'relative_news': relative_news
–
–
–
–
–
Generally this error occurs when we use model queryset at the place of django models object. In the given question you have done the same. "Objects.filter" returns the model query set there can be single or multiple django model objects, but "objects.get" returns single django model object. Or we can use .last() and .first() with "objects.filter".
this error rises if you use queryset or list in your filter params. For example,
News.objects.filter(title = a)
and here if "a" inside a filter is a queryset or list, then this error raises.
–
The problem you are facing is that you first take a filter queryset (a collection of objects from your database) for example: Book.objects.filter(subject='fiction')
)
and then you are trying to use that filter as a parameter (like subject = 'fiction' in the previous one ) to obtain another filter queryset.
Instead you have to use an object using 'get' instead of 'filter' that is the previous example would become
book = Book.objects.get(subject='fiction')
and then use that object as parameter for next filter.
example: Book_shelf = BookShelf.objects.filter(book = book)
I also experienced the same issue, try doing this, instead of:
relative_news = News.objects.filter(tag=tags)
relative_news = News.objects.filter(tag=tags).first()
I got the same error below:
ValueError: The QuerySet value for an exact lookup must be limited to
one result using slicing.
Because I assigned the queryset made by filter() to category
as shown below:
# Here # Here
Product.objects.filter(category=Category.objects.filter(pk=1))
So, I put __in after category
as shown below, then the error was solved:
# ↓ Here
Product.objects.filter(category__in=Category.objects.filter(pk=1))
Or, I assigned the object made by get() to category
as shown below, then the error was solved:
# Here
Product.objects.filter(category=Category.objects.get(pk=1))
list_inst=[]
for i in list_Series:
list_inst += Episodes.objects.filter(id_serie=i.id_serie)
list_Episodes=list_inst
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.