如何訪問 urls.py 中的外鍵?################models.py...class Category(models.Model): title = models.CharField(verbose_name='TITLE', max_length=200) slug = models.SlugField('SLUG', unique=True, allow_unicode=True, help_text='one word for title alias.') ... def __str__(self): return self.titleclass Episode(models.Model): category = models.ForeignKey("Category", verbose_name=("CATEGORY"), on_delete=models.CASCADE) number = models.IntegerField().... meta: ... def __str__(self): ... def get_absolute_url(self): return reverse("manga:episode_detail", kwargs={"slug": self.category.slug, "number": self.number})#####################urls.py... # path('category/<slug:category.slug>/<int:number>', views.EpisodeDetailView.as_view(), name="episode_detail"),]#################views.py...class EpisodeDetailView(DetailView): model = Episode template_name = 'manga/episode_detail.html' context_object_name = 'episode'** **它拋出了“必須使用 URLconf 中的對(duì)象 pk 或 slug 來調(diào)用通用詳細(xì)信息視圖 EpisodeDetailView?!?*我想看情節(jié)的細(xì)節(jié)。但我不能...我嘗試過拋出查詢集,但它不起作用......如何訪問 urls.py 中的外鍵?** # def get_queryset(self): # return Episode.objects.filter(category__slug=self.kwargs['lab']) def get_queryset(self): employee = Category.objects.get(slug=self.kwargs['slug']) print(employee) return Episode.objects.get(category=employee, number=self.kwargs['number'])
通用詳細(xì)視圖 View 必須使用 URLconf 中的對(duì)象 pk 或 slug 來調(diào)用
料青山看我應(yīng)如是
2023-09-26 16:36:56