G
G
gagoman2012-04-05 21:12:45
Python
gagoman, 2012-04-05 21:12:45

Communication between entities in GAE?

Good evening.
I decided to try GAE and the following problem arose:
There are 2 entities: an article and article categories. Article categories contain only one field with their description.
During the creation of the article, there is a choice of a category from the select.
Actually, the question is:
How best to connect them together?
1. Adding the id field seems to be not kosher (correct if not), there is an option to use the code of the form:
[(category.key().id(), category.title) for category in ArticleCategory.all()]
and in the article object itself to store int
2. Use db.ReferenceProperty? But then I don't quite understand how to populate SelectField from
WTForms
Thanks.
PS Most likely it turned out chaotically, sorry. If necessary, I will add
PSS and the second question - please stick with your nose, where in the dock it is written how the datastore changes when the model changes (read - there were 3 fields, one was renamed - is the old data left from the field and is it necessary to somehow migrate them? )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xSkyFoXx, 2012-04-06
@gagoman

You are confusing relational databases and datastore a bit, trying to apply your knowledge of the former in an application to the latter. I'll try to answer your question.
The Datastore is not inherently a relational database. It refers to NoSQL databases, and most of all resembles an object-oriented database model. Any entry in the datastore is a self-sufficient entity (Entity), which consists of a set of attributes (Property, free translation). Models that you inherit to create records can be considered something (albeit very remotely) similar to function factories. Those. models simply allow you to "construct" a set (group) of similar objects. The key word is "similar". There are no requirements for uniformity. There is simply no such thing as a “table” and its “fields”.
Imagine that you have written a model: And created 2 instances: Then in the database, each of the objects will contain information about only 2 attributes! There will be no, relatively speaking, “empty columns”. This is due to the GAE storage architecture. The fact is that there are no restrictions or requirements for processing requests on one server, or storing data on one server, or that one iron responds to 2 consecutive user requests - no. In addition, your 2 requests going one row can even be processed by 2 different data centers.
class Article(db.Model):
title = db.PropertyString()
author = db.PropertyString()
body = db.PropertyString()

art1 = Article(title = 'First title', autor = 'Sechspir')
art1.put()
art2 = Article(title = 'First title', body = 'Second article body')
art2.put()

What way out exists, how to uniquely identify some structure? You probably noticed that each of the entries has a key? This is the same unique identifier that allows you to find your data in data centers. The only unique parameter of the system, "smeared" on millions of machines.
OK. Now to practice.
1) Yes, indeed, using ReferenceProperty is the most “kosher way”. It will allow you to find the desired object by the key link.
2) No, you don't need to do any migrations. This is where the essence of dynamic structures comes into play. You can add and remove properties on the fly. And any of the objects can have its own unique set. This will not generate redundancy in storage. We store objects, and their keys are links where these objects can be taken.
3) How to populate SelectField? Do a prefetch with .filter() and .fetch() exactly as many objects as you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question