在 Django 管理器中,为什么使用 self.get_query_set().get(kwarg=val) 而不是 self.get(kwarg=val) ?
在我问的之前的问题中,Manager的方法如下所示:
def activate(key):
try:
profile = self.get(key=key)
except self.model.DoesNotExist:
return None
if not profile.key_expired():
# -> Activate user
return user
return None
建议使用self.get_query_set( ).get(key=key)
而不是管理器方法中的 self.get(key=key)
。我想知道这是什么原因,因为前者似乎更冗长?
In a prior question I asked, where a Manager's method looked like:
def activate(key):
try:
profile = self.get(key=key)
except self.model.DoesNotExist:
return None
if not profile.key_expired():
# -> Activate user
return user
return None
It was suggested to use self.get_query_set().get(key=key)
instead of self.get(key=key)
within the manager method. I was wondering what the reason for this is, as the former seems much more verbose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜作者就是喜欢啰嗦。没有什么区别。 Manager 类的 get 方法定义为:
您可以在 django/db/models/manager.py 中亲自查看它
I guess the author just likes being verbose. There is no difference. The Manager class' get method is defined as:
You can see it for yourself in django/db/models/manager.py