2013-03-12 1 views
1

많은 기존 인스턴스가있는 엔티티 모델에 목록 속성을 추가했습니다.BadValueError : 속성 X가 필요합니다.

응용 프로그램은 짧은 시간 동안 문제없이 실행되는 실제 환경에 배포 할시
class MyModel(db.Model): 

    new_property = db.ListProperty(item_type=str, default=None) 

다음은 데이터 저장소에서 레코드를 검색하려고으로 BadValueError 오류가 던지는 시작합니다.

app_item = db.get(app_item_key) 

내가 1.7.5를 사용하고 있습니다 :

오류를 던지고 코드는 데이터 저장소에 바로 호출입니다. 파이썬 2.7 런타임.

내가 이것을 막기 위해 무엇을 할 수 있는지, 아니면 적어도 상점에서 데이터를 가져올 수 있도록 잡아 두겠다는 아이디어가 있습니까? 에 따라 그 사람들을 위해

Traceback (most recent call last): 
    File "/base/data/home/apps/app/4-15.365909351579418812/app.py", line 1739, in app_get 
    app_item = db.get(app_item_key) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1533, in get 
    return get_async(keys, **kwargs).get_result() 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result 
    return self.__get_result_hook(self) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1459, in __get_hook 
    entities = rpc.user_data(entities) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 600, in local_extra_hook 
    return extra_hook(result) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1503, in extra_hook 
    model = cls1.from_entity(entity) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1438, in from_entity 
    return cls(None, _from_entity=entity, **entity_values) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 970, in __init__ 
    prop.__set__(self, value) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 614, in __set__ 
    value = self.validate(value) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 3460, in validate 
    value = super(ListProperty, self).validate(value) 
    File "/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 641, in validate 
    raise BadValueError('Property %s is required' % self.name) 
BadValueError: Property new_property is required 

:

new_property = db.ListProperty(item_type=str, default=None) 

읽어야합니다 :

빈 목록에 기본 값을 변경

을 아론 D의 제안에 따라 경찰은, 그래서이 문제를 해결

new_property = db.ListProperty(item_type=str, default=[]) 
+0

검색 호출과 같은 관련성 높은 코드를 게시 할 수 있습니까? 그것이 http://stackoverflow.com/questions/6142439/badvalueerror-property-xxxx-is-required-even-after-the-xxxx-property-has-alrea?rq=1에 느슨하게 관련되어있는 것처럼 보입니다. –

+0

이 대답은 관련이있을 수 있습니다 : http://stackoverflow.com/a/10012905/399704 –

+0

두 경우 모두 이것들은 property와 관련된 required = true와 관련이 있습니다. 그러나 여기에는 해당되지 않습니다 (비록 그것이 그대로도 작동 함에도 불구하고) . – notreadbyhumans

답변

1

, 당신은 ListProperty의 다큐 멘 테이션 코멘트에 코멘트를 볼 수 있습니다 (라인 3428)는 말한다 : 그래서

Note that the only permissible value for 'required' is True.

당신이 그것을 제공하지 않는 경우에도, 그것은 라인 3442처럼 보이는이 자동으로 설정됩니다 : self._require_parameter(kwds, 'required', True)

소스 코드 (라인 3500)에 더 보면,의 정의를 볼 수 있습니다ListProperty A의 :

def empty(self, value): 
    """Is list property empty. 

    [] is not an empty value. 

    Returns: 
     True if value is None, else false. 
    """ 
    return value is None 

나는 오류가 발생할 수 있습니다이 문제를 생각할 수 있지만 테스트를 통해 확인하지 않았습니다.

1) 어떤 이유로 인해 해당 필드에 데이터가 이미 있습니다 (아마도 new_property 이름을 다시 사용하고 있습니까?) 비어 있었기 때문에 오류가 발생했을 수 있습니다. 대신이 문제를 해결하는 방법을 모르겠다. 대신 new_property에 고유 한 이름을 사용하는 것이 좋습니다. post I referenced in my comment은 데이터를 "수정"하는 방법을 설명합니다.

2) 이미 레코드가 있으므로 코드에서 기본값 None을 사용하는 코드를 채우려 고 시도하고 있습니다 (empty() 테스트와 일치 함). 그런 다음 예외가 throw됩니다. 이 경우 기본값 인 []을 대신 입력하면 작동합니다.

+0

문제를 생성하는 방법에 대한 위의 내 대답을 참조하십시오. 엔티티가 이미 None이라는 StringProperty로 작성된 경우 ListProperty의 모델 정의에서 기본값을 []로 변경하면 여전히 동일하게됩니다 이 경우 BadValueError는 속성이 존재하고 ListProperty에 대해 유효하지 않은 값이 None에 있습니다. –

+0

1) new_property는 이전에 사용 된 속성 또는 속성 이름이 아닙니다. 2) 기본값 = 없음에서 기본값 = []으로 변경하면 문제가 해결 된 것으로 보입니다. – notreadbyhumans

0

여기 예제 코드는 정말 확실합니다. 당신이 사용하고있는 것이 아닙니다. 나는 새 속성에 required=True을 가지고있을 것입니다. 그런 다음 필수 등록 정보에 대한 값이없는 이전 레코드를 검색 중입니다. 'required = True`를 삭제하면 오류가 사라집니다. 이 값이 필요하면 제약 조건을 적용하기 전에 필드에 기본값을 추가해야합니다.

*이 None

그래서 나는 당신이 제공 한 정보를 바탕으로 상황을 복제하려고 * ListProperty에 대한 올바른 기본값 없다는 대한 몇 가지 완전한 쓰레기를 제거하고 나는 대답을 가지고있다. 먼저 기본값이 None 인 StringProperty 유형의 이름이 new_property 인 모델을 생성하여 문제를 생성 할 수 있습니다. new_property getting the default of None written, then change the model definition of new_property to ListProperty`에 값이없는 레코드를 put()하고 레코드를 가져옵니다. 동일한 스택 추적을 얻습니다. 아래 쉘 로그를보십시오.

s~lightning-catfish> class MyModel(db.Model): 
... pass 
... 
s~lightning-catfish> x = MyModel() 
s~lightning-catfish> x.put() 
datastore_types.Key.from_path(u'MyModel', 1001L, _app=u's~lightning-catfish') 
s~lightning-catfish> class MyModel(db.Model): 
... new_property = db.ListProperty(item_type=str,default=None) 
... 
s~lightning-catfish> y = db.get(x.key()) 
s~lightning-catfish> y 
<MyModel object at 0x9e09dcc> 
s~lightning-catfish> y.new_property 
[] 
s~lightning-catfish> new_property = db.StringProperty(defaul 
KeyboardInterrupt 
s~lightning-catfish> class MyModel(db.Model): 
... new_property = db.StringProperty(default=None) 
... 
s~lightning-catfish> z = MyModel() 
s~lightning-catfish> z.put() 
datastore_types.Key.from_path(u'MyModel', 2001L, _app=u's~lightning-catfish') 
s~lightning-catfish> class MyModel(db.Model): 
... new_property = db.ListProperty(item_type=str,default=None) 
... 
s~lightning-catfish> a1 = db.get(z.key()) 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 1533, in get 
    return get_async(keys, **kwargs).get_result() 
    File "/home/timh/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result 
    return self.__get_result_hook(self) 
    File "/home/timh/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1459, in __get_hook 
    entities = rpc.user_data(entities) 
    File "/home/timh/google_appengine/google/appengine/api/datastore.py", line 600, in local_extra_hook 
    return extra_hook(result) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 1503, in extra_hook 
    model = cls1.from_entity(entity) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 1438, in from_entity 
    return cls(None, _from_entity=entity, **entity_values) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 970, in __init__ 
    prop.__set__(self, value) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 614, in __set__ 
    value = self.validate(value) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 3460, in validate 
    value = super(ListProperty, self).validate(value) 
    File "/home/timh/google_appengine/google/appengine/ext/db/__init__.py", line 641, in validate 
    raise BadValueError('Property %s is required' % self.name) 
BadValueError: Property new_property is required 
s~lightning-catfish> 

데이터를 수정하려면 낮은 수준에서 데이터에 액세스하고 레코드에 저장된 데이터 형식을 변경해야합니다.

원한다면 모델을 사용하지 않고 엔티티를 가져오고 넣는 코드가 있습니다.

* 마지막 당신은이 모델을 사용하지 않고 객체를 가져 오기 위해 같은 *

다음 코드 또는 무언가를 사용하려고 시도해야합니다. dicts에서 형식 등을 사용하여 기본 데이터를 다시 얻습니다. 그러면 데이터 저장소에있는 내용이 표시됩니다.당신의 추적에서 참조 __init__.py에서 구글 앱 엔진의 소스 코드를 보면

from google.appengine.api import datastore 
from google.appengine.api import datastore_errors 

def get_entities(keys): 
    rpc = datastore.GetRpcFromKwargs({}) 
    keys, multiple = datastore.NormalizeAndTypeCheckKeys(keys) 
    entities = None 
    try: 
     entities = datastore.Get(keys, rpc=rpc) 
    except datastore_errors.EntityNotFoundError: 
     assert not multiple 

    return entities 


x = get_entities([some_key]) 
+0

예는 속성의 직접 복사이며 필수 항목을 사용하고 있지 않습니다. = true. – notreadbyhumans

+0

BadValueError : Property new_property is required는 정확히'required = True' 일 때 무엇인가가 발생합니다. –

+0

OK, None은 ListProperty의 유효한 기본값이 아닙니다. 기본값을'[]'로 변경하십시오. –

관련 문제