How to get great value from one of my models in Google App Engine

I have a model below and I would like to get all the excellent area values. SQL equivalent select distinct area from tutorials

 class Tutorials(db.Model): path = db.StringProperty() area = db.StringProperty() sub_area = db.StringProperty() title = db.StringProperty() content = db.BlobProperty() rating = db.RatingProperty() publishedDate = db.DateTimeProperty() published = db.BooleanProperty() 

I know that in Python I can do

  a = ['google.com', 'livejournal.com', 'livejournal.com', 'google.com', 'stackoverflow.com'] b = set(a) b >>> set(['livejournal.com', 'google.com', 'stackoverflow.com']) 

But this will require that I move the area elements from the request to another list, and then perform installation on the list (it sounds very inefficient), and if I have a separate element that is located at position 1001 in the data warehouse, I would not see it from - beyond the sample limit of 1000.

I would like to get all the individual region values โ€‹โ€‹in my datastore so that I can upload it to the screen as links.

+5
python google-app-engine google-cloud-datastore
source share
3 answers

Datastore cannot do this for you in one request. A data warehouse query always returns a sequential block of results from an index, and an index always consists of all objects of a given type, sorted according to any orders. There is no way for a query to skip items only because one field has duplicate values.

One option is to restructure your data. For example, enter a new entity type representing "area". When you add a tutorial, you create the corresponding "area" if it does not already exist, and when you delete Tutoral, delete the corresponding "area" if there are no tutorials with the same "area". If the amount of teaching aids in this area is stored in each area, this may not be too burdensome (although maintaining things compatible with transactions, etc., It would be rather inconvenient). I expect that the entity key can be based on the realm row itself, which means that you can always perform key searches, rather than queries to retrieve region objects.

Another option is to use the job in the queue or the cron job to periodically create a list of all areas, accumulating it for several queries if necessary, and put the results either in the data warehouse or in memcache. Of course, this means that the list of areas may be temporarily out of date (or if there are constant changes, it may never be fully established), which may or may not be acceptable to you.

Finally, if there are very few areas compared to study guides, you can do this on the fly by requesting the first workbook (sorted by region), and then requesting the first tutorial, whose area is larger than the area in the first place, and so Further. But this requires one request per separate area, so it is unlikely to be fast.

+7
source share

The DISTINCT keyword was introduced in version 1.7.4.

+1
source share

This was asked before , and it was concluded that the use of sets is in order.

0
source share

All Articles