我想从模型中分离一些逻辑并将其分组到属性中,就像Django处理模型管理器(object
属性)一样。事实上,类似于ForeignKey
但没有数据库表示
我有这样的想法:
class Remote(object):
def __init(self, *args, **kwargs)
self.post = ... # how to get to post instance?
def synchronize(self):
# this function requires Post object access
print(self.post.name)
class Post(models.Model):
name = models.CharField(max_length=100)
remote = Remote()
...
for post in Post.objects.all():
post.remote.synchronize()
问题
如何修改上述代码以访问Remote
对象中的Post
对象
附加问题
是否可以确定是否已从Post
实例(post.remote...
–如上所述)或Post
类(Post.remote...
)调用了Remote
对象
您在这里想要的东西可以通过descriptors实现
为了让它工作,您需要在类中定义一个
__get__
方法,该方法要作为另一个类的属性进行访问您的案例的一个简单示例如下所示:
说明:
在上面的代码中,每次调用
Post
模型的属性remote时,RemoteDescriptor
的__get__
方法将被调用。obj的第一个检查是确保描述符是从其他对象调用的,而不是直接调用的。这里需要两个类Remote和RemoteDescriptor,以便您能够在描述符中添加自定义方法,这些方法可以使用dot访问(例如post.remote.calculate()
)还请注意,在第一次调用和所有后续调用时,我将
Remote
的实例放在Post的dict中,对象将从那里返回您还应该检查RealPython上描述符的great article
相关问题 更多 >
编程相关推荐