Skip to content
Advertisement

ValueError at /category/economy/: Field ‘id’ expected a number but got ‘economy’

I try to add new path and this happen “Field ‘id’ expected a number but got ‘economy’.”

in traceback the highlighted line is in the views.py file which i mentioned below.

JavaScript

I am sharing my files plz help me to get rid of the issue. urls.py

JavaScript

here i used str:cats, yet it shows “Field ‘id’ expected a number but got ‘economy’.”

views.py

JavaScript

“category_posts = Post.objects.filter(category=cats)” this line of code shows in traceback

models.py

JavaScript

categories.html

JavaScript

I am confused it demands. can someone help me to solve it plz.

Advertisement

Answer

Its because you are querying it wrong: So instaed of doing this:

JavaScript

Try something with this query. I’m supposing you want to query all the posts of a specific category that will be coming from your URL.

JavaScript

PS: When you don’t know what your ForeignKey related_name should be. Then go for the plural name of the model. Like in the current case:

JavaScript

This way we can query like this category_posts = category.posts.all()

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement