[英]Arguments of DetailView methods and usage of pk_url_kwarg
I'm currently watching a course on Django and wondering about the following code: 我正在观看关于Django的课程并想知道以下代码:
class RestaurantDetailView(DetailView):
queryset = Restaurant.objects.all()
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
return context
def get_object(self, *args, **kwargs):
rest_id = self.kwargs.get('rest_id')
obj = get_object_or_404(Restaurant, id=rest_id)
return obj
1.) Why is the instructor of this course using *args
in the get_context_data
method but in the source code of django the get_context_data
only has **kwargs
1.)为什么本课程的讲师在get_context_data
方法中使用*args
,但在django的源代码中, get_context_data
只有**kwargs
Source Code of get_context_data method get_context_data方法的源代码
2.) Furthermore the get_object
method. 2.)此外还有get_object
方法。 Why does he use *args
and **kwargs
but the method in Django only has a queryset
argument 为什么他使用*args
和**kwargs
但Django中的方法只有一个queryset
参数
Source Code of get_object method get_object方法的源代码
3.) And my last question, why isn't the just using the pk_url_kwarg
variable to change the name of pk
to rest_id
3.)和我的上一个问题,为什么不是只使用pk_url_kwarg
变量将pk
的名称更改为rest_id
I rewrote this code an it still works but I'm really new to Django and I'm not sure if misunderstood something. 我重写了这段代码,它仍然可以工作,但我是Django的新手,我不确定是否误解了一些东西。
class RestaurantDetailView(DetailView):
pk_url_kwarg = 'rest_id'
queryset = Restaurant.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
As a Django user, def get_context_data(self, *args, **kwargs):
and def get_object(self, *args, **kwargs):
look unusual to me. 作为Django用户, def get_context_data(self, *args, **kwargs):
和def get_object(self, *args, **kwargs):
看起来不寻常。 The code will work because the same args and kwargs are passed to super()
. 代码将起作用,因为相同的args和kwargs传递给super()
。 You could argue that it makes the code more robust because it will still work if Django changes the signature in a future version. 您可以争辩说,它使代码更加健壮,因为如果Django在未来版本中更改签名,它仍然可以工作。 However I would prefer to use the same signatures as the parent class. 但是我更喜欢使用与父类相同的签名。
You are correct that you could use pk_url_kwarg
instead of overriding get_object
. 你是正确的,你可以使用pk_url_kwarg
而不是覆盖get_object
。 The pk_url_kwarg
should be the name of the kwarg in the URL pattern, so in this case it should be pk_url_kwarg = 'rest_id'
. pk_url_kwarg
应该是URL模式中kwarg的名称,因此在这种情况下应该是pk_url_kwarg = 'rest_id'
。 The advantage of pk_url_kwarg
is that it simplifies the code. pk_url_kwarg
的优点是它简化了代码。 The disadvantage is that it's less obvious how the object is being fetched if you're not familiar with Django's class-based-views. 缺点是如果你不熟悉Django的基于类的视图,那么获取对象的方式就不那么明显了。
There's a couple more changes you could make. 你可以做出更多的改变。 Instead of queryset = Restaurant.objects.all()
you can simply set model
, because get_queryset
defaults to self.model.objects.all()
. 您可以简单地设置model
,而不是queryset = Restaurant.objects.all()
,因为get_queryset
默认为self.model.objects.all()
。
Finally, the get_context_data
method doesn't do anything apart from printing, so I would remove it entirely once I'd finished debugging. 最后, get_context_data
方法除了打印之外没有做任何事情,所以一旦我完成调试,我会完全删除它。
Putting that together, you get: 把它放在一起,你会得到:
class RestaurantDetailView(DetailView):
model = Restaurant
pk_url_kwarg = 'rest_id'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.