如何在Google App Engine中为模型定义独特的属性? [英] How do I define a unique property for a Model in Google App Engine?

查看:77
本文介绍了如何在Google App Engine中为模型定义独特的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些属性是唯一的。如何才能做到这一点?



有没有像 unique = True



我使用Google App Engine for Python。

解决方案

没有内置的制约因素确定一个值是唯一的。但是,您可以这样做:

  query = MyModel.all(keys_only = True).filter('unique_property',value_to_be_used)
entity = query.get()
如果实体:
异常('unique_property必须具有唯一值!')

我使用 keys_only = True ,因为它不会为实体获取数据而略微提高性能。 p>

更有效的方法是使用单独的模型,不包含其键名由属性名+值组成的字段。然后,您可以使用 get_by_key_name 来获取这些组合键名称中的一个或多个,如果您得到一个或多个不是 - None 值,你知道有重复的值(并且检查哪些值不是 None,),你会知道哪些值不是唯一的。)






作为评论中提到的 onebyone ,这些方法–他们先拿到后放自然 - ndash;运行风险并发问题。理论上,可以在检查现有值后立即创建实体,然后检查后的代码仍将执行,从而导致重复值。为防止发生这种情况,您必须使用交易:交易 - Google App Engine






如果您希望检查所有实体的唯一性交易,你必须使用第一种方法将它们全部放在同一个组中,这将非常低效。对于事务处理,使用第二种方法:

$ p $ class UniqueConstraint(db.Model):
@classmethod
def check(cls,model,** values):
#创建一个用作实体组的伪密钥。
parent = db.Key.from_path(model.kind(),'unique-values')

#构建要测试的键名列表。
key_names = []
键值:
key_names.append('%s:%s'%(key,values [key]))

def txn():
result = cls.get_by_key_name(key_names,parent)
用于测试结果:
if test:返回false
for key_names中的key_name:
uc = cls(key_name = key_name,parent = parent)
uc.put()
return True

return db.run_in_transaction(txn)

UniqueConstraint.check(...)会假设每个键/值对必须是唯一的才能返回成功。该交易将为每种模型类型使用一个实体组。通过这种方式,事务可以同时在多个不同的字段中可靠(仅适用于一个字段,这会更简单)。另外,即使您在一个或多个模型中具有相同名称的字段,它们也不会与彼此。


I need some properties to be unique. How can I achieve this?

Is there something like unique=True?

I'm using Google App Engine for Python.

解决方案

There's no built-in constraint for making sure a value is unique. You can do this however:

query = MyModel.all(keys_only=True).filter('unique_property', value_to_be_used)
entity = query.get()
if entity:
    raise Exception('unique_property must have a unique value!')

I use keys_only=True because it'll improve the performance slightly by not fetching the data for the entity.

A more efficient method would be to use a separate model with no fields whose key name is made up of property name + value. Then you could use get_by_key_name to fetch one or more of these composite key names and if you get one or more not-None values, you know there are duplicate values (and checking which values were not None, you'll know which ones were not unique.)


As onebyone mentioned in the comments, these approaches – by their get first, put later nature – run the risk concurrency issues. Theoretically, an entity could be created just after the check for an existing value, and then the code after the check will still execute, leading to duplicate values. To prevent this, you will have to use transactions: Transactions - Google App Engine


If you're looking to check for uniqueness across all entities with transactions, you'd have to put all of them in the same group using the first method, which would be very inefficient. For transactions, use the second method like this:

class UniqueConstraint(db.Model):
    @classmethod
    def check(cls, model, **values):
        # Create a pseudo-key for use as an entity group.
        parent = db.Key.from_path(model.kind(), 'unique-values')

        # Build a list of key names to test.
        key_names = []
        for key in values:
            key_names.append('%s:%s' % (key, values[key]))

        def txn():
            result = cls.get_by_key_name(key_names, parent)
            for test in result:
                if test: return False
            for key_name in key_names:
                uc = cls(key_name=key_name, parent=parent)
                uc.put()
            return True

        return db.run_in_transaction(txn)

UniqueConstraint.check(...) will assume that every single key/value pair must be unique to return success. The transaction will use a single entity group for every model kind. This way, the transaction is reliable for several different fields at once (for only one field, this would be much simpler.) Also, even if you've got fields with the same name in one or more models, they will not conflict with each other.

这篇关于如何在Google App Engine中为模型定义独特的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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