Default value for ndb.KeyProperty - python

I have these two models:
#################
### Usergroup ###
#################
class Usergroup(ndb.Model):
group_name = ndb.StringProperty(indexed = False, required = True)
is_admin_group = ndb.BooleanProperty(indexed = False, required = False, default = False)
############
### User ###
############
class User(ndb.Model):
fb_id = ndb.StringProperty(indexed = True, required = True)
fb_access_token = ndb.TextProperty(indexed = False, required = True)
email = ndb.StringProperty(indexed = True, required = True)
first_name = ndb.StringProperty(indexed = False, required = True)
last_name = ndb.StringProperty(indexed = False, required = True)
gender = ndb.StringProperty(indexed = False)
group_key = ndb.KeyProperty(indexed = False, required = False, kind = Usergroup, default = ndb.Key(Usergroup, 'member'))
join_date = ndb.DateTimeProperty(indexed = True, auto_now_add = True)
last_login = ndb.DateTimeProperty(indexed = True, auto_now_add = True, auto_now = False)
#app.route('/user/login', methods=['POST'])
def user_login():
me = exchange_token_me(request.form['accessToken'])
if me is not False:
user = user_find_or_register(me)
if user is not None:
register_session(user)
return 'success'
return 'error'
def user_find_or_register(user):
qry = User.query(User.fb_id == user['id'])
existing_user = qry.get()
if existing_user is not None:
existing_user.fb_access_token = user['access_token']
existing_user.fb_id = user['id']
existing_user.email = user['email']
existing_user.last_login = datetime.datetime.now()
existing_user.put()
return existing_user
new_user = User()
new_user.fb_id = user['id']
new_user.fb_access_token = user['access_token']
new_user.email = user['email']
new_user.first_name = user['first_name']
new_user.last_name = user['last_name']
new_user.gender = user['gender']
new_user.last_login = datetime.datetime.now()
#new_user.group_key = ndb.key(Usergroup, 'member')
key = new_user.put()
saved_user = key.get()
#key.delete() # DEBUG
if saved_user is not None:
return saved_user
def register_session(user):
session['fb_id'] = user.fb_id
session['first_name'] = user.first_name
session['last_name'] = user.last_name
session['group_key'] = user.group_key
session['loggedin'] = True
The Usergroup model has a small unique string as entity key. There is already a Usergroup whose key is 'member'
Whenever we create/save a user, it should use the key to the 'member' usergroup, but we get this error instead:
TypeError: Key('Usergroup', 'member') is not JSON serializable
Traceback:
ERROR 2016-10-18 14:32:40,572 app.py:1587] Exception on /user/login [POST]
Traceback (most recent call last):
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1643, in full_dispatch_request
response = self.process_response(response)
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1864, in process_response
self.save_session(ctx.session, response)
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 926, in save_session
return self.session_interface.save_session(self, session, response)
File "/var/www/mywebsite/public_html/lib/flask/sessions.py", line 359, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 565, in dumps
payload = want_bytes(self.dump_payload(obj))
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 847, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 550, in dump_payload
return want_bytes(self.serializer.dumps(obj))
File "/var/www/mywebsite/public_html/lib/flask/sessions.py", line 85, in dumps
return json.dumps(_tag(value), separators=(',', ':'))
File "/var/www/mywebsite/public_html/lib/flask/json.py", line 126, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/lib/python2.7/json/__init__.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 209, in encode
chunks = list(chunks)
File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
o = _default(o)
File "/var/www/mywebsite/public_html/lib/flask/json.py", line 83, in default
return _json.JSONEncoder.default(self, o)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Key('Usergroup', 'member') is not JSON serializable
ERROR 2016-10-18 14:32:40,593 main.py:178] An error occurred during a request.
Traceback (most recent call last):
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1643, in full_dispatch_request
response = self.process_response(response)
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 1864, in process_response
self.save_session(ctx.session, response)
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 926, in save_session
return self.session_interface.save_session(self, session, response)
File "/var/www/mywebsite/public_html/lib/flask/sessions.py", line 359, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 565, in dumps
payload = want_bytes(self.dump_payload(obj))
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 847, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "/var/www/mywebsite/public_html/lib/itsdangerous.py", line 550, in dump_payload
return want_bytes(self.serializer.dumps(obj))
File "/var/www/mywebsite/public_html/lib/flask/sessions.py", line 85, in dumps
return json.dumps(_tag(value), separators=(',', ':'))
File "/var/www/mywebsite/public_html/lib/flask/json.py", line 126, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/lib/python2.7/json/__init__.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 209, in encode
chunks = list(chunks)
File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
o = _default(o)
File "/var/www/mywebsite/public_html/lib/flask/json.py", line 83, in default
return _json.JSONEncoder.default(self, o)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Key('Usergroup', 'member') is not JSON serializable
INFO 2016-10-18 14:32:40,610 module.py:788] default: "POST /user/login HTTP/1.1" 500 27
UPDATE:
After Dan has spotted the problem, the solution is in the following function:
def register_session(user):
session['fb_id'] = user.fb_id
session['first_name'] = user.first_name
session['last_name'] = user.last_name
session['group_key'] = user.group_key.id() # Thanks Dan
session['loggedin'] = True

FWIW, a quick test with your code as models.py shows this to be working just fine, at least on the development server:
from models import User
user = User(email='email', username='username')
user.put()
produced:
This worked without even having a Usergroup entity - a key can exist without a matching entity. Of course, trying to follow the link to the Usergroup in the datastore viewer fails:
Traceback (most recent call last):
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/home/usr_local/google_appengine/google/appengine/tools/devappserver2/admin/admin_request_handler.py", line 96, in dispatch
super(AdminRequestHandler, self).dispatch()
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/usr_local/google_appengine_1.9.40/lib/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/usr_local/google_appengine/google/appengine/tools/devappserver2/admin/datastore_viewer.py", line 741, in get
entities = [datastore.Get(entity_key)]
File "/home/usr_local/google_appengine/google/appengine/api/datastore.py", line 671, in Get
return GetAsync(keys, **kwargs).get_result()
File "/home/usr_local/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
return self.__get_result_hook(self)
File "/home/usr_local/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1717, in __get_hook
entities = extra_hook(entities)
File "/home/usr_local/google_appengine/google/appengine/api/datastore.py", line 640, in local_extra_hook
raise datastore_errors.EntityNotFoundError()
EntityNotFoundError
So you might want to show your actual code creating the entity and the full traceback, something else may be going on.
Your traceback indicates that the problem is not with creating the entity, but with saving your session:
File "/var/www/mywebsite/public_html/lib/flask/app.py", line 926, in save_session
return self.session_interface.save_session(self, session, response)
It appears you included in the session content the key object (possibly by including the entire User entity?), which is what causes the failure. For that purpose keys need to be serialized and you can use key.urlsafe() for that. See this answer for an example: https://stackoverflow.com/a/34835074/4495081
If indeed you included the entire User entity in the session you can just include its urlsafe key instead.
Yup, this is the source of your problem:
session['group_key'] = user.group_key
change it to:
session['group_key'] = user.group_key.urlsafe()
And you'll retrieve it like this:
urlsafe_key = session.get('group_key')
if urlsafe_key:
group_key = ndb.Key(urlsafe=urlsafe_key)

Related

django-storage with S3: TypeError: a bytes-like object is required, not 'str'

I have a function that exports a CSV to S3:
def export_csv(report_id):
# MyModel has a JsonField called `json_data`
items = MyModel.objects.all()
data = [x.json_data for x in items]
df = pd.DataFrame(data)
file_buffer = io.StringIO()
# QUOTE_ALL to avoid separators from throwing off columns
df.to_csv(
file_buffer,
index=False,
encoding='utf-8',
quotechar='"',
quoting=csv.QUOTE_ALL,
)
report.report_data_file.save(
"data.csv", ContentFile(file_buffer.getvalue())
)
I'm using the following FieldField:
class PrivateMediaStorage(S3Boto3Storage):
location = settings.AWS_PRIVATE_MEDIA_LOCATION
default_acl = 'private'
file_overwrite = False
custom_domain = False
class Report(BaseModel):
...
report_data_file = models.FileField(
storage=PrivateMediaStorage(),
upload_to=export_location,
null=True,
blank=True,
)
This is working fine in my local environment, where PrivateMediaStorage is defined as
class PrivateMediaStorage(FileSystemStorage):
pass
In production, where I am using django-storages, I am seeing the following error when running this function:
TypeError: a bytes-like object is required, not 'str'
Here is the full traceback:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 650, in __protected_call__
return self.run(*args, **kwargs)
File "/code/apps/benchmark/tasks.py", line 45, in export_report_data_task
report.export_json_data()
File "/code/apps/benchmark/models.py", line 470, in export_json_data
export_csv(self.id)
File "/code/apps/benchmark/utils/export_report_data.py", line 46, in export_csv
report.report_data_file.save(
File "/usr/local/lib/python3.8/site-packages/django/db/models/fields/files.py", line 87, in save
self.name = self.storage.save(name, content, max_length=self.field.max_length)
File "/usr/local/lib/python3.8/site-packages/django/core/files/storage.py", line 52, in save
return self._save(name, content)
File "/usr/local/lib/python3.8/site-packages/storages/backends/s3boto3.py", line 495, in _save
self._save_content(obj, content, parameters=parameters)
File "/usr/local/lib/python3.8/site-packages/storages/backends/s3boto3.py", line 510, in _save_content
obj.upload_fileobj(content, ExtraArgs=put_parameters)
File "/usr/local/lib/python3.8/site-packages/boto3/s3/inject.py", line 619, in object_upload_fileobj
return self.meta.client.upload_fileobj(
File "/usr/local/lib/python3.8/site-packages/boto3/s3/inject.py", line 539, in upload_fileobj
return future.result()
File "/usr/local/lib/python3.8/site-packages/s3transfer/futures.py", line 106, in result
return self._coordinator.result()
File "/usr/local/lib/python3.8/site-packages/s3transfer/futures.py", line 265, in result
raise self._exception
File "/usr/local/lib/python3.8/site-packages/s3transfer/tasks.py", line 255, in _main
self._submit(transfer_future=transfer_future, **kwargs)
File "/usr/local/lib/python3.8/site-packages/s3transfer/upload.py", line 558, in _submit
self._submit_multipart_request(
File "/usr/local/lib/python3.8/site-packages/s3transfer/upload.py", line 622, in _submit_multipart_request
for part_number, fileobj in part_iterator:
File "/usr/local/lib/python3.8/site-packages/s3transfer/upload.py", line 270, in yield_upload_part_bodies
fileobj, full_size = self._get_upload_part_fileobj_with_full_size(
File "/usr/local/lib/python3.8/site-packages/s3transfer/upload.py", line 343, in _get_upload_part_fileobj_with_full_size
return six.BytesIO(data), len(data)
TypeError: a bytes-like object is required, not 'str'

Python: str(bytes object) in jsonify causes TypeError: Object of type 'bytes' is not JSON serializable

I sent a request to my API but it throws TypeError: Object of type 'bytes' is not JSON serializable. It also returns a 500 INTERNAL SERVER ERROR.
Code:
def login():
data = request.json
if(data['token'] != 'xxxxxxxxxxx'):
return jsonify(), 401
user = User.objects(npm=data['npm']).first()
if(user == None):
del data['token']
user = User(**data)
user.major = Major.objects(name=data['major']).first()
user.role = 'user'
user.save()
token = jwt.encode({
'user_id': str(user.id)
}, secret_key, algorithm='HS256')
return jsonify({
'user_id': str(user.id),
'token': token,
'major_id': str(user.major.id)
}), 200
Traceback:
Traceback (most recent call last):
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\anisha\env\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\anisha\env\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\anisha\env\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "c:\users\anisha\env\lib\site-packages\flask_cors\decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "C:\Users\Anisha\sunjadv2-server\app.py", line 69, in login
'major_id': str(user.major.id)
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 321, in jsonify
dumps(data, indent=indent, separators=separators) + '\n',
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 179, in dumps
rv = _json.dumps(obj, **kwargs)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "c:\users\anisha\env\lib\site-packages\flask\json\__init__.py", line 81, in default
return _json.JSONEncoder.default(self, o)
File "C:\Users\Anisha\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable
When I do print("major_id: " + str(user.major.id)) it will print major_id: 5b55e986f15bf336e0b820fe to console. Why does str(user.major.id) seen as bytes type in jsonify? I've tried to delete 'major_id': str(user.major.id) but then line 'token': token will cause the same error.
Thank you.
It seems that your data may not be properly decoded. You could try to decode your data before jsonifying it.
For example, instead of using token directly, try using: token.decode('utf-8')

Flask and JSON related

I'm studying Flask and now want to dump some data to json and print a piece of data to the web:
#app.route('/home')
def index():
if 'piece' in session:
m = session['piece']
session.clear()
# online_users = mongo.db.GOOG.find({'date': '2017-02-01'})
return render_template('index.html')
#app.route('/query', methods=['POST'])
def query():
# stockName = request.form['stockName']
print(request.json)
stockName = request.json['stockName']
stock = mongo.db[stockName]
m = stock.find({})
session['piece'] = m.clone()
mClone = m.clone()
docs = []
for doc in m:
one = json_util.dumps(doc, default=json_util.default)
docs.append(one)
docs = json_util.dumps(docs)
print type(docs)
s = str(mClone[4][u'high'])
print s
return s
the output is:
{u'stockName': u'GOOG'}
<type 'str'>
838.0
127.0.0.1 - - [11/Apr/2017 00:38:28] "POST /query HTTP/1.1" 500 -
Traceback (most recent call last):
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1632, in finalize_request
response = self.process_response(response)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 1858, in process_response
self.save_session(ctx.session, response)
File "G:\Python\Python27\lib\site-packages\flask\app.py", line 924, in save_session
return self.session_interface.save_session(self, session, response)
File "G:\Python\Python27\lib\site-packages\flask\sessions.py", line 363, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "G:\Python\Python27\lib\site-packages\itsdangerous.py", line 565, in dumps
payload = want_bytes(self.dump_payload(obj))
File "G:\Python\Python27\lib\site-packages\itsdangerous.py", line 847, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "G:\Python\Python27\lib\site-packages\itsdangerous.py", line 550, in dump_payload
return want_bytes(self.serializer.dumps(obj))
File "G:\Python\Python27\lib\site-packages\flask\sessions.py", line 85, in dumps
return json.dumps(_tag(value), separators=(',', ':'))
File "G:\Python\Python27\lib\site-packages\flask\json.py", line 123, in dumps
rv = _json.dumps(obj, **kwargs)
File "G:\Python\Python27\lib\site-packages\simplejson\__init__.py", line 397, in dumps
**kw).encode(obj)
File "G:\Python\Python27\lib\site-packages\simplejson\encoder.py", line 291, in encode
chunks = self.iterencode(o, _one_shot=True)
File "G:\Python\Python27\lib\site-packages\simplejson\encoder.py", line 373, in iterencode
return _iterencode(o, 0)
File "G:\Python\Python27\lib\site-packages\flask\json.py", line 80, in default
return _json.JSONEncoder.default(self, o)
File "G:\Python\Python27\lib\site-packages\simplejson\encoder.py", line 268, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <pymongo.cursor.Cursor object at 0x035026B0> is not JSON serializable
I'm a bit curious about the error. It's like happening between print s and return s. Or would it be possible that after the post of query, /home is loaded again, but in this case, s should be returned to my web page.
Solved.
Seems session will excute by the end of the function, and m has been iterated, thus error appears.
Don't know if this explains correctly.
At first you need from flask import jsonify.
print type(docs)
s = str(mClone[4][u'high'])
print s
return jsonify({data: s})
here is the flask documentation link with an example:
http://flask.pocoo.org/docs/0.12/patterns/jquery/

Appengine throwing BadRequestError: The property.name is the empty string

I have an application that has worked successfully in the past. Today however, it's started throwing an error when I try write to datastore. For example, I'm creating a new entity of this model
class EventInstallment(ndb.Model):
somekey = ndb.KeyProperty()
somename = ndb.StringProperty(default = "")
start_date = ndb.DateTimeProperty()
notes = ndb.StringProperty("")
moderator_approved = ndb.BooleanProperty(default = True)
added_by = ndb.KeyProperty()
created = ndb.DateTimeProperty(auto_now_add = True)
using this code
ins = somemodel()
ins.somename = "26-september-2016"
ins.somekey = the_key
ins.start_date = datetime.datetime.now()
ins.put()
and this exception gets thrown.
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/admin/__init__.py", line 363, in post
exec(compiled_code, globals())
File "<string>", line 8, in <module>
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3451, in _put
return self._put_async(**ctx_options).get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 383, in get_result
self.check_success()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 427, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 824, in put
key = yield self._put_batcher.add(entity, options)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 427, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 358, in _put_tasklet
keys = yield self._conn.async_put(options, datastore_entities)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 513, in _on_rpc_completion
result = rpc.get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
return self.__get_result_hook(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1881, in __put_hook
self.check_rpc_success(rpc)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1373, in check_rpc_success
raise _ToDatastoreError(err)
BadRequestError: The property.name is the empty string.
Any idea what this issue might be? It looks like a change in GAE ndb - as it worked as recently as 1 month ago...
Shouldn't notes = ndb.StringProperty("") be: notes = ndb.StringProperty(default = "") ?

BadRequestError: projection and keys_only cannot both be set

Hi i am getting error on this line. Its working in local.
I just deleted all the entities at appspot.
#query line
sent_list = db.GqlQuery("SELECT email FROM MailListNew WHERE active = True")
#error line
sent_count = sent_list.count()
projection and keys_only cannot both be set
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~merdiven-mailservice/1.361030076545076265/index.py", line 89, in post
sent_count = sent_list.count()
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2120, in count
result = raw_query.Count(limit=limit, **kwargs)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1670, in Count
batch = self.GetBatcher(config=config).next()
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2671, in next
return self.next_batch(self.AT_LEAST_ONE)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2708, in next_batch
batch = self.__next_batch.get_result()
File "/base/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 "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2450, in __query_result_hook
self._batch_shared.conn.check_rpc_success(rpc)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1216, in check_rpc_success
raise _ToDatastoreError(err)
BadRequestError: projection and keys_only cannot both be set
You need to change the query to:
sent_list = db.GqlQuery("SELECT * FROM MailListNew WHERE active = True")
or (better):
sent_list = db.GqlQuery("SELECT __key__ FROM MailListNew WHERE active = True")

Categories