33👍
The following will work:
def newsDetailView(request, news_pk):
news = get_object_or_404(News, id=news_pk)
relative_news = News.objects.filter(tag__id__in=news.tag.all())
13👍
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”.
- [Django]-Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- [Django]-Add additional options to Django form select widget
- [Django]-When to use or not use iterator() in the django ORM
11👍
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.
- [Django]-Django 1.8 Run a specific migration
- [Django]-Django rest framework create nested objects "Models" by POST
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
4👍
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)
- [Django]-Manifest: Line: 1, column: 1, Syntax error on Chrome browser
- [Django]-What is the Simplest Possible Payment Gateway to Implement? (using Django)
- [Django]-Django in / not in query
1👍
I also experienced the same issue, try doing this, instead of:
relative_news = News.objects.filter(tag=tags)
to
relative_news = News.objects.filter(tag=tags).first()
- [Django]-How to verify if object exist in manytomany
- [Django]-Django multiprocessing and database connections
- [Django]-TypeError: count() takes exactly one argument
1👍
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))
- [Django]-Control the size TextArea widget look in django admin
- [Django]-Loading fixtures in django unit tests
- [Django]-Passing lists or tuples as arguments in django raw sql
0👍
YOU CAN DO LIKE THIS
list_inst=[]
for i in list_Series:
list_inst += Episodes.objects.filter(id_serie=i.id_serie)
list_Episodes=list_inst
- [Django]-Get Timezone from City in Python/Django
- [Django]-A QuerySet by aggregate field value
- [Django]-Limit number of characters with Django Template filter