Нужно вывести данные по типам, написал такой запрос, но появляется ошибка (Field 'id' expected a number but got 'web').
На страницу я вывожу данные из категорий, но сейчас в категории выводятся все features, а мне надо что б на странице web выводились только те features у которых в поле type есть web, а на странице android, те у которых есть android в type.
models
from django.db import models
from django.urls import reverse
# Create your models here.
class Type(models.Model):
name = models.CharField(max_length=150)
slug = models.SlugField(max_length=150, unique=True, verbose_name='Url')
def get_absolute_url(self):
return reverse('type', kwargs={'slug': self.slug})
def __str__(self):
return self.name
class Meta:
verbose_name = 'Type'
verbose_name_plural = 'Types'
class Category(models.Model):
name = models.CharField(max_length=150)
title = models.CharField(max_length=150)
def __str__(self):
return self.name
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
class Feature(models.Model):
name = models.CharField(max_length=40)
slug = models.SlugField(max_length=40, unique=True, verbose_name='Id')
image = models.ImageField(upload_to='photos/features/')
hours = models.IntegerField(default=1)
category = models.ForeignKey('Category', on_delete=models.CASCADE, related_name='feature')
type = models.ManyToManyField('Type', blank=False, related_name='feature')
class Meta:
verbose_name = 'Feature'
verbose_name_plural = 'Features'
views
class HomePage(ListView):
model = Feature
template_name = 'app_calculator/index.html'
context_object_name = 'features'
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = Type.objects.get(slug=self.kwargs['slug'])
context['categories'] = Category.objects.prefetch_related().filter(feature__type=self.kwargs['slug'])
return context
html
{% block content %}
<div class="container bg-secondary text-dark pb-4 mb-4">
<h1 class="text-center pt-4">Estimate my app</h1>
<h2 class="text-center text-light pt-4">Web development</h2>
<h4 class="text-center pt-4">Select the items below which best describe your app and the features you
require.</h4>
{% for category in categories %}
<div class="container container-cat bg-dark mt-4 mb-4">
<div class="cat-title pt-4 pb-4">
<h3 class="text-center text-light">{{ category.title }}</h3>
</div>
<div class="cat-btns mb-4">
{% for f in category.feature.all %}
<div class="btn-item mb-4">
<img src="{{ f.image.url }}" data-bs-toggle="tooltip" data-bs-placement="top"
title="{{ f.name }}">
<span hidden="true">{{ f.hours }}</span>
</div>
{% endfor %}
</div>
</div>
{% endfor %}