这是我的用户对象
class User(BaseModel):
_email = CharField(max_length=255, unique=True)
_first_name = CharField(max_length=255)
_last_name = CharField(max_length=255)
_profile_image = CharField(max_length=500, default=None)
_gender = CharField(max_length=255, default=None)
def __init__(self, email, first_name, last_name):
self._email = email
self._first_name = first_name
self._last_name = last_name
self._profile_image = None
self._gender = None
def serialize(self):
user_map = {}
for attr, value in self.__dict__.items():
user_map[attr[1:]] = value
return user_map
这是 BaseModel 对象
class BaseModel(Model):
_create_at = DateTimeField(default=datetime.now())
_modified_at = DateTimeField(default=datetime.now())
class Meta:
database = db
这就是数据库的定义方式,
db = SqliteDatabase('cmp_v0.db')
def before_request_handler():
db.connect()
def after_request_handler():
db.close()
这是用户资源,
class UserResource:
def on_get(self, req, res):
user = User('someone@email.com', 'Firstname', 'Lastname')
res.body = json.dumps(user.serialize())
这是创建 REST 端点的代码
api = falcon.API()
api.add_route('/user', UserResource())
当我转到网址http://127.0.0.1:8000/user时 ,我收到以下错误,
instance._data[self.att_name] = value
TypeError: 'NoneType' object does not support item assignment
我究竟做错了什么。