Home > Net >  Field 'id' expected a number but got 'category.id'
Field 'id' expected a number but got 'category.id'

Time:06-27

I'm trying to make a category in the DB, but when I try to access the category with the model that entered. it showing me this Error: ValueError at /category/Django/

Django is category I entered with the model I made using a ForeignKey.

I tried a different method for the category but it didn't work either. so I think the problem is in the db.sqlite3 file or in the migrations files. I can't delete the sql3 db file, I have data that I can't easy replaces. I don't think the problem is in the settings.py, I tried to change it before and got the same outcome.

the full Error:

\Python39\site-packages\django\db\models\lookups.py", line 25, in __init__
    self.rhs = self.get_prep_lookup()
  File "C:\Users\HPi5\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\fields\related_lookups.py", line 117, in get_prep_lookup
    self.rhs = target_field.get_prep_value(self.rhs)
  File "C:\Users\HPi5\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\models\fields\__init__.py", line 1825, in get_prep_value
    raise e.__class__(
ValueError: Field 'id' expected a number but got 'published'.

here is the views.py for the category

class CatListView(ListView):
    template_name = "Blog/category.html"
    context_object_name = 'catlist'

    def get_queryset(self):
        content = {
            'cat': self.kwargs['category'],
            'posts': Post.objects.filter(category__name=self.kwargs['category']).filter(category='published')
        }
        return content


def category_list(request):
    category_list = Category.objects.exclude(name='default')
    context = {
        "category_list": category_list,
    }

the urls.py

from django.urls import path
from . import views

urlpatterns = [
 path("category/<category>/", views.CatListView.as_view(), name="category"),
]

and for the category.html

{% for post in catlist.posts %}
{% endfor %}

Update:

models.py

from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    name = models.CharField(max_length=100)


    class Meta:
        ordering = ('name',)
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def __str__(self):
        return self.name

    

class Post(models.Model): 
 
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    werkstoffnummer = models.CharField(max_length=100)

    def __str__(self):
        return self.werkstoffnummer

    def get_absolute_url(self):
        return reverse("post-detail", kwargs={"pk": self.pk})

CodePudding user response:

The Post.objects.filter(category__name=self.kwargs['category']) part alone is already filtering the posts based on the category from your key word arguments. If you only want to filter posts with published category name only, you can category__name="Published".

Not entirely sure if that answers your question so comment if thats not it.

CodePudding user response:

Your problem is here:

.filter(category='published')

You are trying to filter a QuerySet of Post objects by category. But category is a ForeignKey field that accepts only id or Category objects. You are trying to pass there string - 'published'. It cannot be that way.

  • Related