python - Datastore One-to-Many Google App Engine -
i'm struggling display array items in template. started entity relationships in gae , i'm not finding suitable solutions on so. have perform query first in handler. if so, how coz i'm struggling logic.
datastore project
entities example display
datastore employees
entities example display
project.py
class project(ndb.model): projectid = ndb.integerproperty(required=true) title = ndb.stringproperty(required=true) description = ndb.stringproperty(required=true) startat = ndb.datetimeproperty(indexed=true) endat = ndb.datetimeproperty() isfullday = ndb.booleanproperty() days = ndb.keyproperty(kind='companyday', repeated=true) employees = ndb.keyproperty(kind='employees', repeated=true) client = ndb.keyproperty(kind='client') class employees(ndb.model): name = ndb.stringproperty() role = ndb.stringproperty()
template
data
fetched backend through angular's http
service
<md-content class="md-padding projects" layout="row" layout-wrap> <md-card ng-repeat="data in controller.projects" flex="20" ng-click="editproject(data)"> <img src="" alt=""> <md-card-content> <div class="project-info"> <h2 class="md-title">{{ data.title }}</h2> <p>{{ data.description }}</p> <p>{{ data.employees }}</p> </div> </md-card-content> </md-card> </md-content>
python code responds /comments
class commentsjsonhandler(webapp2.requesthandler): @classmethod def route(cls): """ name: index, template: / """ return webapp2.route('/comments', handler=cls, name='_commments_') def get(self): projects = project.query() def date_handler(obj): print obj if isinstance(obj, datetime): return obj.isoformat() else: return str(obj) self.response.out.write(json.dumps([dict(proj.to_dict(), **dict(id=proj.key.id())) proj in projects], default=date_handler))
from angular - code requests server
var vm = this; vm.projects = []; $http.get('/comments') .then(function(result) { console.log(result); vm.projects = result.data; });
displaying employees datastore.
you change date_handler handle ndb.key render employee collection in returned json
def date_handler(obj): print obj if isinstance(obj, datetime): return obj.isoformat() elif isinstance(obj, ndb.key): return obj.get().to_dict() else: return str(obj)
you may want change name of method not doing date_handling more
Comments
Post a Comment