Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructuredProperty inside another StructuredProperty. How to?

I have an Entity which has a variable amount of another Entity in it (so I'm usingStructured Property, repeated=True), but that one property can hold variable amount of single entity kind as well. So my code looks like this:

class Property(ndb.Model):
    name    = ndb.StringProperty()
    cost    = ndb.FloatProperty()
    type    = ndb.StringProperty()

class SpecialProperty(ndb.Model):
    name       = ndb.StringProperty()
    properties = ndb.StructuredProperty(Property, repeated=True)
    type       = ndb.StringProperty() 

class Hotel(ndb.Model):
    specialProperties  = ndb.StructuredProperty(SpecialProperty, repeated=True)

But when I try this GAE throws an error. "TypeError: This StructuredProperty cannot use repeated=True because its model class (SpecialProperty) contains repeated properties (directly or indirectly)."

So how can I bypass this? I really need to have this flexible structure.

Big thanks in advance.

like image 263
momijigari Avatar asked May 24 '26 13:05

momijigari


1 Answers

Although a StructuredProperty can be repeated and a StructuredProperty can contain another StructuredProperty, beware: if one structured property contains another, only one of them can be repeated. A work-around is to use LocalStructuredProperty, which does not have this constraint (but does not allow queries on its property values).

https://developers.google.com/appengine/docs/python/ndb/properties#structured

With LocalStructuredProperty you will have the same structure, but you will not be able to filter by this properties. If you really need to do queries by one of this properties -- try put it into another entity.

like image 60
Dmytro Sadovnychyi Avatar answered May 26 '26 04:05

Dmytro Sadovnychyi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!