如何访问 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.title
class 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 中的对象 pk 或 slug 来调用通用详细信息视图 EpisodeDetailView。”**
我想看情节的细节。但我不能...
我尝试过抛出查询集,但它不起作用......
如何访问 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'])
胡子哥哥
相关分类