Lazy-loading large/complex model properties in Google App Engine - google-app-engine

Let's say I'm modeling a website where a web page would be represented by a PageModel, like so:
class PageModel(db.Model):
name = db.StringProperty()
parentPage = db.SelfReferenceProperty()
content = db.TextProperty()
I'd like to be able to pull a list of all my page objects, in order to render menus, etc., but without having to pull in the content for all the items. How would you model this object so that you could pull in the content only when you needed it? Would it require a 1-to-1 reference relationship with a separate 'content' model? And if so, would you make the reference on the page object or on the content object?

You could move the content property into a new model (PageContentModel). I would implement the reference by having the parent of the PageContentModel be the PageModel (using the parent property of db.Model). This allows you to modify both of them in a single transaction (as they are in a single entity group).
One benefit of modeling things with the PageContentModel having a reference to the PageModel (as opposed to the PageModel having a reference to the PageContentModel) is that if you ever need content to be larger than 1MB you can do so by allowing each PageModel to have 1 or more PageContentModel objects and you would just split your content into 1MB chunks and write each chunk to a different PageContentModel instance. To be able to get the content back you would need the PageContentModel objects to have an "order" property associated with them so you can re-build your content in the correct order.
To query for the PageContentModel instances related to a PageModel you would use the ancestor filter like this:
PageContentModel.all().ancestor(page_model_instance)
As suggested by #Nick another way to do this would be to use the files api to write the content to a blob in the blobstore and then link that blob to the PageModel by having a BlobReferenceProperty on the PageModel. I have now had a chance to try this and it is working pretty well (despite it being an experimental feature). This would allow your content to be very large and, under the new pricing model, is actually cheaper than storing your content inside the datastore model.
Updated Feb 7, 2012 to include suggestion from #Nick about the blobstore.

Related

ManyToManyField is not getting saved in Wagtail Page

I have a subclass of Wagtail Page class that has field of django ManyToManyField type. When I try to create a new instance of my page object, I get a list of objects that the the ManyToManyField points to and I am able select multiple items. However, after creating that page when I try to edit the same page, it seems no data got saved for the ManyToMany field. I know in Django ModelAdmin one have to override the save_related() to save ManyToMany field data. Is there a similar method for the Wagtail Page model?
You should define the field as a ParentalManyToManyField relation, as per the example here: http://docs.wagtail.io/en/v1.13.1/getting_started/tutorial.html#categories
This is a variant of ManyToManyField which is able to keep track of the relation in memory, allowing it to work in situations such as previewing and saving as draft (where it doesn't get saved to the normal database records).
I was able to use the 'after_edit_page' and 'after_create_page' hooks to save the data for the page's ManyToMany fields.

SuiteCommerce Advanced - Show a custom record on the PDP

I am looking to create a feature whereby a User can download any available documents related to the item from a tab on the PDP.
So far I have created a custom record called Documentation (customrecord_documentation) containing the following fields:
Related item : custrecord_documentation_related_item
Type : custrecord_documentation_type
Document : custrecord_documentation_document
Description : custrecord_documentation_description
Related Item ID : custrecord_documentation_related_item_id
The functionality works fine on the backend of NetSuite where I can assign documents to an Inventory item. The stumbling block is trying to fetch the data to the front end of the SCA webstore.
Any help on the above would be much appreciated.
I've come at this a number of ways.
One way is to create a Suitelet that returns JSON of the document names and urls. The urls can be the real Netsuite urls or they can be the urls of your suitelet where you set up the suitelet to return the doc when accessed with action=doc&id=_docid_ query params.
Add a target <div id="relatedDocs"></div> to the item_details.tpl
In your ItemDetailsView's init_Plugins add
$.getJSON('app/site/hosting/scriptlet.nl...?action=availabledoc').
then(function(data){
var asHtml = format(data); //however you like
$("#relatedDocs").html(asHtml);
});
You can also go the whole module route. If you created a third party module DocsView then you would add DocsView as a child view to ItemDetailsView.
That's a little more involved so try the option above first to see if it fits your needs. The nice thing is you can just about ignore Backbone with this approach. You can make this a little more portable by using a service.ss instead of the suitelet. You can create your own ssp app for the function so you don't have to deal with SCAs url structure.
It's been a while, but you should be able to access the JSON data from within the related Backbone View class. From there, within the return context, output the value you're wanting to the PDP. Hopefully you're extending the original class and not overwriting / altering the core code :P.
The model associated with the PDP should hold all the JSON data you're looking for. Model.get('...') sort of syntax.
I'd recommend against Suitelets for this, as that's extra execution time, and is a bit slower.
I'm sure you know, but you need to set the documents to be available as public as well.
Hope this helps, thanks.

Are collections required?

Sorry this is a noob question but if I only need some initial data when the application first loads is a collection always needed or can the model fetch the data and pass it directly to the view?
Nothing in backbone is really "required". It's a very thin, more-than-one-way-to-do-it framework. Jeremy recommends data that can be bootstrapped in the initial page load be handled that way, so your HTML could include you initial data as JSON in a <script> tag. You can pass that JSON to a Backbone.Collection (if it's a list of similar records) or a new Backbone.Model (if it's a single domain object). You can also just use a model and call model.fetch to get your initial data. Model vs. Collection is more about single domain object with name/value pairs vs list of many objects where iterating, sorting, filtering are common.

cakephp: abstract classes, factory, return objects

I would need an idea or two how I would do this in cakephp (using latest version)
I am building a web based game where you will be able to collect Items
Without a framework I would have an abstract base item class that every item would extend to
And when displaying for example a inventory i would factory all items the user currently have and then return a object for each item.
classes...
BaseItem
WeaponItem
HealingItem
etc..
How would I do this in cakephp? Would I go for a model for each item class ... and how would i factor to get the object? ...
Assuming you're using a database as the data store, presumably you will use a single table for all items the player can collect? If so, you probably want a single Model class.
It's possible to have an inheritance hierarchy for models in CakePHP if you want. But you can often achieve sharing of Model logic using a Behaviour.

Create lists of multiple users as a model property in Google App Engine

I would like to create a Group model in Google App Engine and then have an attribute where I can create a list of UserReferences. The documentation said:
"A property can have multiple values, represented in the datastore API as a Python list. The list can contain values of any of the value types supported by the datastore."
Would I implement this by creating:
class Group(db.Model):
group_list = db.ListProperty(users.User)
Or might I be better served by simply listing the user entity keys?
http://code.google.com/appengine/docs/python/datastore/entitiesandmodels.html
keys are better placed in ReferenceProperty and their purpose is to create relationships between two kinds.
You can simply create the listproperty and as your list grows keep adding listitems to it.
class Group(db.Model):
group_list = db.ListProperty()
This depends on your use-case. If you already have a User model, to store additional data about your users, then using a db.ListProperty(Key) for User model keys is probably your best option.

Resources