Discuss / Python / 按照github最新的代码,测试访问代码应该这么写

按照github最新的代码,测试访问代码应该这么写

Topic source

叫小小兽

#1 Created at ... [Delete] [Delete and Lock User]
import asyncio
import www.orm as orm
from www.models import User, Blog, Comment

loop = asyncio.get_event_loop()
async def test():
    await orm.create_pool(user='root', password='123456', db='awesome', loop=loop)

    u = User(name='Test', email='test@example.com', passwd='1234567890', image='about:blank')

    await u.save()

loop.run_until_complete(test())

泽屁

#2 Created at ... [Delete] [Delete and Lock User]

您好,我按照你的测试代码运行,会在

class Model(dict,metaclass=ModelMetaclass):

    def __init__(self,**kw):

        super().__init__(**kw)

    def __getattr__(self,key):

        try:

            return self[key]

这里显示KeyError'id'

好像在创建user的时候必须把所有的参数都写进去,不能调用默认的uuid和time.time。请问这是为什么呀?

    async def save(self):
        args=list(map(self.getValueOrDefault,self.__fields__)) #获取实例对象的初始化值用做参数
        args.append(self.getValueOrDefault(self.__primary_key__))
        rows=await execute(self.__insert__,args)
        if rows !=1:
            logging.warn(f'插入数据,影响行数:{rows}')
    

把orm里面的save方法获取主键值的参数也改成getValueDefault()就不会报错了


  • 1

Reply