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

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

问题描述

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



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



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

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

解决方案


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

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


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


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


$ b

  profile = UserProfile.get_by_id('my_unique_name')
如果配置文件:
#显示错误消息说用户已经存在。




最后,我尝试在DjangoForms模型上创建一个key_name字段,使用上面的ndb模型作为元类,所以我可以使用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天全站免登陆