我创建了一个pk字段(不是由模型自动创建的)。 当我想使用这个PK名称(在我的情况下是school_id)时,我收到了这个错误:
Generic detail view SchoolDetailView must be called with either an object pk or a slug.
虽然我正在使用正确的pk名称(school_pk),但我在我的模型中使用了url。 我的代码如下: models.py:
from django.db import models
from django.urls import reverse
class School(models.Model):
school_pk = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
principal = models.CharField(max_length=256)
location = models.CharField(max_length=256)
def __str__(self):
return str(self.name)
def get_absolute_url(self):
return reverse("basic_app:school_detail",kwargs={'school_pk':self.school_pk})
views.py:
class SchoolListView(ListView):
model = models.School
template_name = 'basic_app/school_list.html'
class SchoolDetailView(DetailView):
context_object_name = 'school'
model = models.School
template_name = 'basic_app/school_detail.html'
urls.py:
urlpatterns = [
path('',views.SchoolListView.as_view(),name='school_list'),
path('<int:school_pk>/',views.SchoolDetailView.as_view(),name='school_detail'),]
当我尝试去学校_detail页面时,例如转到http://127.0.0.1:8000/basic_app/1/ 我有这个错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/basic_app/1/
Django Version: 2.0
Exception Type: AttributeError
Exception Value:
Generic detail view SchoolDetailView must be called with either an object pk or a slug.
我曾尝试使用get_object和/或get_queryset,但我相信我的做法是错误的。如果任何人可以帮助那将是伟大和高度赞赏。谢谢。 注意:我不想从我的模型中省略school_pk字段并使用自动生成的PK。
答案 0 :(得分:2)
添加library(dplyr)
bind_rows(anti_join(full, dat1, by="ID"), semi_join(dat1, full, by="ID"))
以查看pk_url_kwarg = 'school_pk'
。默认情况下,它设置为SchoolDetailView
。
pk