I'm creating a blog application and I added a category for its blog posts. When adding a new category it can be can be done without any problem. But the problem is I can't find posts for a specific category ID and get an error like
Field 'id' expected a number but got 'health'
But before I updated field category charafield to forenkeyfied it worked without any problem.
Here is my code:
class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
updated_on = models.DateTimeField(auto_now= True)
content = SummernoteTextField(blank=True, null=True)
created_on = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(choices=STATUS, default=0)
image = models.ImageField(upload_to='images',null=True, blank=True)
category = models.ForeignKey('blog.category', on_delete=models.SET_NULL, null=True, blank=True)
class category(models.Model):
name = models.CharField(max_length=80)
def __str__(self):
return self.name
views.py
def CategoryView(request, cats):
category_posts = Post.objects.filter(category=cats.replace('-', ' '))
return render(request, 'categories.html', {'cats':cats.replace('-', ' '), 'category_posts':category_posts})
forms.py
class PostForm(forms.ModelForm):
category = forms.ModelChoiceField(queryset=category.objects.all().order_by('name'))
class Meta:
model = Post
fields = ('title', 'category','author', 'content', 'image','status')