What is the good db property, for an keys's ar

2019-08-21 10:05发布

For the moments bars represents a String with all the Bar ids with ';' separator

like 6;5;9;15

class Foo(db.Model):
    id = db.StringProperty(multiline=True)
    name = db.StringProperty(multiline=True)   
    bars = db.TextProperty()

class Bar(db.Model):
    id = db.StringProperty(multiline=True)
    name = db.StringProperty(multiline=True)

What must I do to explicit Foo contains some bars in db? And also how is going to look my :

f = Foo()
f.name= 'aName'
f.id = '3615'
f.bars = '6;5;9;15'
f.put()

2条回答
beautiful°
2楼-- · 2019-08-21 10:12

For a start, you never want to store multiple values inside a single textfield. AppEngine's datastore supports multi-valued properties (such as ListProperty): you should use those.

That said, there's no multi-valued equivalent of ReferenceProperty built in. This article on the AppEngine documentation site gives a good example of how to model a list of keys.

查看更多
够拽才男人
3楼-- · 2019-08-21 10:14

Assuming this is a many-to-many relationship, you can use db.ListProperty(db.Key) for a list of the Bars' Keys, or, if you really want to just store the integer IDs, db.ListProperty(int).

If each Bar can only be a bar of a single foo, it's probably better to use a ReferenceProperty in the Bar entity, then use the automatic backreference in Foo to get a query for all of the bars.

By the way, naming a property "id" and making it a multiline string is probably a Bad Idea.

查看更多
登录 后发表回答