python - How do I define a unique property for a Model in Google App Engine? -


I need some properties to be unique. How can I get it?

What is something like unique = true ?

I am using Google App Engine for Python.

/ div>

There is no inherent barrier to ensuring that a value is unique. You can do this though:

  query = MyModel.all (keys_only = True) .filter ('unique_property', value_to_be_used) unit = query.get () if entity: increase exception (' Unique_property  

I use keys_only = true because it will improve performance by not meeting the unit's data.

A A more efficient method would be to use a different model with any such field, whose key name is of the property name + value, then you can choose one or two of these composite key names. To get that you can use get_by_key_name and if you do not get one or more - the none value, you know that there are duplicate values ​​which are not values none , you will know which was not unique.)


As mentioned in the comments one billion comments, these approaches & Ndash; Get them first, later nature and ndash; Running risk concurrency issues In principle, an entity can be made only after examining an existing value, and after the check the code will still be executed, thereby duplicating the values. To stop this, you have to use the transaction:


If you want to check the uniqueness among all entities with the transaction, then you have to give to everyone Using the first method in the same group of them, which would be very inefficient. For transactions, use another method like this:

  class UniqueConstraint (db.Model): @classmethod def check (CLS, model, ** value): # Pseudo for use -Create a parent as a unit group; parent = db.k.a.prom_path (model. Kind (), 'unique-value') # Make a list of key names for the test. Value for key: key_names.append ('% s:% s'% (key, values ​​[key]) def txn (): results = test.exe_key_name (key_names, parent) For testing purposes: If the exam: return false for key_name in key_name: uc = cls (key_name = key_name, parent = parent) uc.put () returns the correct return db.run_in_transaction (txn)  

UniqueConstraint.check (...) Assume that every single key / value pair must be unique to return success.The transaction will use one unit group for each model type. In the E is reliable for different areas (for only one field, it will be very easy.) Also, even if you find a field with the same name in one or more models, then they do not struggle with it. Will each other.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -