gpt4 book ai didi

python - 需要 ValueError : Attribute Users. 请求

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:02 27 4
gpt4 key购买 nike

我又需要帮助了。谁能告诉我为什么会出现此错误?我使用 Flask 和 pony orm 以及 python 3.4。我的文件很少。

用户.py
from app import models
from pony.orm import *

class UsersAPI:
@staticmethod
@db_session
def is_registered(e_mail):
"""
This method for checking registered user or not
:param e_mail:
:return:
"""

user = select(o for o in models.Users if o.email == e_mail)[:]
print(user)

UsersAPI.is_registered("evgenusov96@yandex.ru")

文件模型.py
from pony.orm import *
import config

db = Database()

class Users(db.Entity):
id = PrimaryKey(int, auto=True)
email = Required(str, 100)
password = Required(str, 32)
name = Required(str, 15)
surname = Required(str, 20)
city = Required(City, nullable=True)
sex = Required(int)
request = Required(str, 250, nullable=True)
hobby = Required(str, 250, nullable=True)
about = Required(str, 250, nullable=True)
status_message = Required(str, 100, nullable=True)
user1 = Set("Friends", reverse="user1")
user2 = Set("Friends", reverse="user2")
author = Set("Messages", reverse="author")
addressee = Set("Messages", reverse="addressee")

我遇到的这个错误:

    C:\Python34\python.exe C:/Users/Евгений/PycharmProjects/langunet/run.py
Traceback (most recent call last):
File "C:/Users/Евгений/PycharmProjects/langunet/run.py", line 2, in <module>
from app import app
File "C:\Users\Евгений\PycharmProjects\langunet\app\__init__.py", line 4, in <module>
from app import routes
File "C:\Users\Евгений\PycharmProjects\langunet\app\routes.py", line 2, in <module>
from app.users import *
File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 21, in <module>
UsersAPI.is_registered("evgenusov96@yandex.ru")
File "<string>", line 2, in is_registered
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 347, in new_func
try: return func(*args, **kwargs)
File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 18, in is_registered
user = get(o for o in models.Users if o.email == e_mail)
File "<string>", line 2, in get
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4260, in get
return make_query(args, frame_depth=3).get()
File "<string>", line 2, in get
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4531, in get
objects = query[:2]
File "<string>", line 2, in __getitem__
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4721, in __getitem__
return query._fetch(range=(start, stop))
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4426, in _fetch
used_attrs=translator.get_used_attrs())
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3228, in _fetch_objects
real_entity_subclass, pkval, avdict = entity._parse_row_(row, attr_offsets)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3258, in _parse_row_
avdict[attr] = attr.parse_value(row, offsets)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1329, in parse_value
val = attr.validate(row[offset], None, attr.entity, from_db=True)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1611, in validate
if obj is None: throw(ValueError, 'Attribute %s is required' % attr)
File "C:\Python34\lib\site-packages\pony\utils.py", line 126, in throw
raise exc
ValueError: Attribute Users.request is required

这可能如何解决?谢谢你!

最佳答案

如果您将 User.request 属性定义为 Required,但在表中相应的列实际上是可选的(即可以为 null),则可能会发生此错误。在加载对象期间,Pony 会检查对象的内部状态是否一致。由于缺少必需属性的值,Pony 会生成异常。

我认为我们需要通过以下两种方式之一改进 Pony 的行为:

  • 即使需要一个属性,Pony 也应该允许从数据库中加载该属性的空值。
  • 另一种方法是使错误消息更加清晰。

关于python - 需要 ValueError : Attribute Users. 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com