ValueError:属性Users.request是必需的

时间:2015-04-06 06:26:52

标签: python orm pony ponyorm

我需要再次帮助。任何人都可以告诉我为什么会收到此错误?我使用Flask和pony orm以及python 3.4。我的文件很少。

users.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")

文件models.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

如何解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:2)

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

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

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