所以我有2模型類:AppEngine,實體丟失?
class Profile(db.Model):
"""Profiles are merely lighter versions of Users. The are only used for the
purpose of notification
"""
created_at = db.DateTimeProperty(auto_now_add=True)
created_by = None # TODO User class
name = db.StringProperty()
email = db.EmailProperty()
phone = db.PhoneNumberProperty()
class Notification(db.Model):
LEVELS = {
'INFO': 'INFO',
'WARNING': 'WARNING',
'CRITICAL': 'CRITICAL'
}
created_by = None # TODO user class
created_at = db.DateTimeProperty(auto_now_add=True)
profile = db.ReferenceProperty(Profile, collection_name='notifications')
level = db.StringProperty()
這是我的JSONencoder樣子:
class JsonEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj, Profile):
return dict(key=obj.key().id_or_name(),
name=obj.name, email=obj.email, phone=obj.phone)
elif isinstance(obj, Limits):
return None
else:
return json.JSONEncoder.default(self, obj)
基本上,可以通知分配到配置文件,這樣當通知火災,所有的與該通知相關的配置文件將被通知。
在我的html,我有一個表格,允許用戶創建一個信息通報:
<form action="{{ url_for('new_notification') }}" method=post>
<!-- If disabled=true we won't send this in the post body -->
Name:
<input name='name' type='text' value='Select from above' disabled=true />
<br/>
<!-- if type=hidden html will still send this in post body -->
<input name='key' type='hidden' value='Select from above' />
Type:
<select name='level'>
{% for k,v in notification_cls.LEVELS.iteritems() %}
<option value="{{ v }} ">{{ k }}</option>
{% endfor %}
</select>
<br/>
<input type="submit" value="Submit">
</form>
不過,我看到了一些奇怪的事情在我的方法發生在創建通知:
@app.route('/notifications/new/', methods=['GET', 'POST'])
def new_notification():
"""Displays the current notifications avaiable for each profiles
"""
if request.method == 'GET':
return render_template('new_notification.html',
notification_cls=Notification)
else:
profile_key_str = request.form.get('key', None)
level = request.form.get('level', None)
if not profile_key_str or not level:
abort(404)
profile_key = db.Key.from_path('Profile', profile_key_str)
profile = Profile.get(profile_key)
print profile # This results in None??
notification = Notification(parent=profile) # Ancestor for strong consistency
notification.profile = profile_key
notification.level = level
notification.put()
return redirect(url_for('list_notifications'), code=302)
所以用戶可以在new_notifications頁面上創建一個配置文件。之後,我的服務器將通過ajax將新創建的實體密鑰發送到包含該表單的相同頁面。這將設置隱藏的輸入值。
不知何故,在我的new_notification方法中,配置文件不存在!!!!我有點懷疑AppEngine的「最終一致性」政策,但我一直在等待30分鐘,結果仍然是無。
任何想法我做錯了什麼?
編輯:
我忘了,包括它調用JSONEncoder我的JS方法
@app.route('/profiles/', methods=['GET'])
def ajax_list_profiles():
def _dynatable_wrapper(profiles):
return {'records': profiles,
'queryRecordCount': len(profiles),
'totalRecordCount': len(profiles)}
name = request.args.get('queries[search]', None)
if not name:
profiles = db.Query(Profile).order('name').fetch(None)
return json.dumps(_dynatable_wrapper(profiles), cls=JsonEncoder)
else:
profiles = db.Query(Profile).filter("name =", name).order('name').fetch(None)
return json.dumps(_dynatable_wrapper(profiles), cls=JsonEncoder)
配置文件鍵應該如何進出模板?你沒有看到它被傳入,而隱藏的字段具有不相關的字符串值。 –
@DanielRoseman我更新了 – disappearedng