为 App Engine NDB 模型指定 key_name 的最佳方法是什么? [英] What's the best way to specify a key_name for App Engine NDB Model?

查看:25
本文介绍了为 App Engine NDB 模型指定 key_name 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 ndb 模型,其中每条记录都有一个唯一的字段名称".我想将此字段定义为 key_name 字段并使用它来查找记录.我是否必须包含名称字段,或者我可以以某种方式将 key_name 字段设置为用户可以指定的任意字符串,只要它是唯一的?

我正在考虑使用 Model.get_or_insert 来确保旧记录不会被覆盖,但是有没有办法判断返回值是新创建的还是预先存在的?如果用户输入重复名称,我希望能够显示错误消息.

最后,我尝试在使用上述 ndb 模型作为元类的 DjangoForms 模型上创建一个 key_name 字段,以便我可以使用 djangoforms 进行验证/渲染,但由于某些原因我定义的字段没有显示.

类 UserProfileForm(djangoforms.ModelForm):key_name = djangoforms.StringProperty()元类:模型 = 用户资料

解决方案

我是否必须包含名称字段,或者我可以以某种方式将 key_name 字段设置为用户可以指定的任意字符串,只要它是唯一的?

您可以将您的唯一键名称作为 id 参数传递给模型构造函数:profile = UserProfile(id='my_unique_name').

<块引用>

我正在考虑使用 Model.get_or_insert 来确保旧记录不会被覆盖,但是有没有办法判断返回值是新创建的还是预先存在的?如果用户输入重复名称,我希望能够显示错误消息.

使用Model.get_by_id().如果找不到模型,它将返回一个模型实例或 None:

profile = UserProfile.get_by_id('my_unique_name')如果个人资料:# 显示错误信息说用户已经存在.

<块引用>

最后,我尝试在使用上述 ndb 模型作为元类的 DjangoForms 模型上创建一个 key_name 字段,以便我可以使用 djangoforms 进行验证/渲染,但由于某些原因我定义的字段没有显示.

我不知道 DjangoForms 是如何工作的,但很可能它们与 NDB 不兼容.您需要创建自己的验证逻辑.

I'm trying to create an ndb model where each record has an unique field "name". I would like to define this field as the key_name field and use it to look up the records. Do I have to include a name field or can I somehow set the key_name field to an arbitrary string that the user can specify as long as it's unique?

I'm thinking of using Model.get_or_insert to make sure that old records don't get overwritten, but is there a way to tell if the return value is newly created or pre-existing? I want to be able to display an error message if the user entered a duplicate name.

Lastly, I tried to create a key_name field on a DjangoForms model that uses the above ndb model as the metaclass so I can use djangoforms for validation/rendering but for some reason my defined fields don't show up.

class UserProfileForm(djangoforms.ModelForm): key_name = djangoforms.StringProperty() class Meta: model = UserProfile

解决方案

Do I have to include a name field or can I somehow set the key_name field to an arbitrary string that the user can specify as long as it's unique?

You can pass your your unique key name as the id parameter to model constructor: profile = UserProfile(id='my_unique_name').

I'm thinking of using Model.get_or_insert to make sure that old records don't get overwritten, but is there a way to tell if the return value is newly created or pre-existing? I want to be able to display an error message if the user entered a duplicate name.

Use Model.get_by_id(). It will return a model instance or None if a model is not found:

profile = UserProfile.get_by_id('my_unique_name')
if profile:
    # display error message saying that the user already exists.

Lastly, I tried to create a key_name field on a DjangoForms model that uses the above ndb model as the metaclass so I can use djangoforms for validation/rendering but for some reason my defined fields don't show up.

I don't know how DjangoForms work, but most likely they are not compatible with NDB. You will want to create your own validation logic.

这篇关于为 App Engine NDB 模型指定 key_name 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆