from lotan import Lotan
from lotan.models import Model, IntField, StringField
# Define models
class Question(models.Model):
id = models.IntField(is_key=True)
title = models.StringField()
member_id = models.IntField()
def resolve_by_id(self):
return {
'title': 'This is title string',
'member_id': 1,
}
q = Question(id=1)
assert q.title == 'This is title string'
assert q.member_id == 1
Fields are mostly lazy loaded and resolved by resolve funcitons.
Note that not every non-key-field need a corresponding resolve funciton. the result returned by resolve_by_
functions will be automatically applied to model's fields, which means if the resolve_by_id
function in the above example return a dict which contains gender
data, the gender
field of the Member instance will be set to it automatically event if there is no funciton named resolve_gender
.
test_member.py
def test_init_by_id(self, rpc_member):
rpc_member.get_member.return_value = FAKE_MEMBER
member = Member(id=FAKE_MEMBER.id)
self.assertEqual(member.id, FAKE_MEMBER.id)
self.assertEqual(member.fullname, FAKE_MEMBER.fullname)
rpc_member.get_member.assert_called_once_with(member_id=FAKE_MEMBER.id)
Lotan
base.py
__init__(self, **kwargs)
接收到了 kwargs = {'id' : 1}
, 使用 _extract_seed_key(kwargs)
将 'id'
另存为 seed_key
;
__eq__()
calls run_key_resolver()
-
run_key_resolver()
runs the key resolver- update
_exist_state
的值RAN_EXIST
; - 得到有关于 key (id) 的 resolver,
seed_key_resolver = self.get_key_resolver(self.seed_key)
seed_key_resolver = getattr(self, 'resolve_by_%s' % key_name, None)
- 通过
query.py
中的 ResolverManager 运行resolve_by_key
returned_key_result = self.resolver_manager.retrieve_field( key_name=self.seed_key, key_value=self.seed_value, field_name=self.seed_key, field_resolver=seed_key_resolver, session=self.session )
- dispatch_key_result()
- set_field_value()
- update
-
retrieve_field()
calls run_resolver(field_resolver)- 获取函数对象的arg。
return self.run_resolver( field_resolver, *resolver_args, session=session )
-
run_resolver()
: All resolver should call this to solve the problem.
网友评论