如何解决对属性和获取器使用相同的名称
我有以下课程/型号:
class Recipe(db.Model):
...
user_id = db.Column(db.ForeignKey(("users.id")),nullable=False,index=True)
author = db.relationship("User",uselist=False,back_populates="diets")
...
# Permissions
def can_view(self,user = None) -> bool:
if user is None:
user = current_user
return self.author == user
我可以使用recipe.can_view()
或recipe.can_view(some_user)
,但我希望能够呼叫recipe.can_view
而不是recipe.can_view()
,但我不知道有什么好的解决方案那个。
谢谢
解决方法
看起来我无法做到这一点(至少没有弄乱)。
因此,我接受了@zvone在评论中提出的解决方案:
我会选择
can_view(user)
和can_current_user_view
所以,我的代码是:
def can_view(self,user) -> bool:
return self.is_author(user) or user.is_admin or self.is_public
@property
def can_current_user_view(self) -> bool:
return self.can_view(user=current_user)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。