ValueError:属性Users.request是必需的

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError:属性Users.request是必需的相关的知识,希望对你有一定的参考价值。

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

users.朋友

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:Python34python.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ЕвгенийPycharmProjectslangunetapp\__init__.py", line 4, in <module>
    from app import routes
  File "C:UsersЕвгенийPycharmProjectslangunetapp
outes.py", line 2, in <module>
    from app.users import *
  File "C:UsersЕвгенийPycharmProjectslangunetappusers.py", line 21, in <module>
    UsersAPI.is_registered("evgenusov96@yandex.ru")
  File "<string>", line 2, in is_registered
  File "C:Python34libsite-packagesponyormcore.py", line 347, in new_func
    try: return func(*args, **kwargs)
  File "C:UsersЕвгенийPycharmProjectslangunetappusers.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:Python34libsite-packagesponyutils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:Python34libsite-packagesponyormcore.py", line 4260, in get
    return make_query(args, frame_depth=3).get()
  File "<string>", line 2, in get
  File "C:Python34libsite-packagesponyutils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:Python34libsite-packagesponyormcore.py", line 4531, in get
    objects = query[:2]
  File "<string>", line 2, in __getitem__
  File "C:Python34libsite-packagesponyutils.py", line 88, in cut_traceback
    return func(*args, **kwargs)
  File "C:Python34libsite-packagesponyormcore.py", line 4721, in __getitem__
    return query._fetch(range=(start, stop))
  File "C:Python34libsite-packagesponyormcore.py", line 4426, in _fetch
    used_attrs=translator.get_used_attrs())
  File "C:Python34libsite-packagesponyormcore.py", line 3228, in _fetch_objects
    real_entity_subclass, pkval, avdict = entity._parse_row_(row, attr_offsets)
  File "C:Python34libsite-packagesponyormcore.py", line 3258, in _parse_row_
    avdict[attr] = attr.parse_value(row, offsets)
  File "C:Python34libsite-packagesponyormcore.py", line 1329, in parse_value
    val = attr.validate(row[offset], None, attr.entity, from_db=True)
  File "C:Python34libsite-packagesponyormcore.py", line 1611, in validate
    if obj is None: throw(ValueError, 'Attribute %s is required' % attr)
  File "C:Python34libsite-packagesponyutils.py", line 126, in throw
    raise exc
ValueError: Attribute Users.request is required

这怎么可能解决?谢谢!

答案

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

我认为我们需要在两种方式中改进Pony行为:

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

以上是关于ValueError:属性Users.request是必需的的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:属性Users.request是必需的

JSON ValueError:期望属性名称:第 1 行第 2 列(字符 1)

使用Flask创建注册表单 - AttributeError:'ValueError'对象没有属性'name'

ValueError 将 sklearn 和 pandas 用于决策树?

ValueError:找到样本数量不一致的输入变量:[2839,14195]

Python 属性错误:“int”对象没有属性“reshape”