Create lists of multiple users as a model property in Google App Engine - 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.

Related

Search/Filter Dropdown in Django Admin Panel for standard CharField

I am using Django to build data models including a model Company. Some of the fields belonging to this model are limited to set choices using the choices='' argument. Some of these fields have a large number of choices, for example a country CharField which lists all countries. Finding the right value among the long list can be tedious so I want to be able to search across the given choice values. This is easy to do for ForeignKey/ManytoMany fields using autocomplete_fields = [] as seen in the attached screenshot (from a different model) but can't seem to find a method for implementing this with a normal CharField with lots of choices. This seems like something that should be built into the Django for the admin panel but I can't find anything within the docs. Please how can I implement a search/filter dropdown for any given (non FK/m2m) fields? Thanks in advance. If there's anymore information I can provide let me know and I will.
country = models.CharField(max_length=128, choices=COUNTRY_CHOICES, null=True)
Model code added. This COUNTRY_CHOICES array is what I would like to be able to select from in a searchable dropdown list.

Field Type of entity-query not working, correctly retrieves list of content-items but incorrectly stores them as 'Empty Slot"

I have an Authors App which has x amount of authors. I have another app and have configured an Field Input-Type entity-query in it which pulls from the Authors App. It does this correctly and I can select multiple authors. However upon save, when I go to retrieve a content item which should contain the selected authors, I am given "empty slot" for the place of each author
Real entity fields are Entity relations, and they enforce validity. So they only work with entities in the same app, as that's kind of a sealed scope. This is important that Apps can ensure export/import and still work for all standard use cases.
To reference entities of another app you must use strings instead. This can be done using the string-query field which has the same functionality.
The only downside is that your code will need to then look up the entity in the other app using the id or guid (whichever you store) in Razor.

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.

app engine ndb - how to load entity by key using id?

I am trying to load an entity by key using the id it was assigned by the datastore but I don't see any api method to do this (using NDB). I thought I would be able to make a Key from an integer id and use key.get() to load the entity, but I don't see a way to make a key from just an id. I suspect I am missing something obvious here. How should I load an entity where I only know the id of it?
Another way: ndb.Key(YourModel, id).get().
YourModel.get_by_id() gets a model instance by id.
here the docs:
https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_get_by_id
don't think you can't get an entity by id without knowing the kind because instances of different Model classes can have the same id/key_name
Models in NDB don't define their key type as part of the model. This is nifty in that you can have one given model type that is accessible through multiple different kinds of keys and parents, which makes them more flexible. But it's somewhat problematic because it isn't always clear what the key represents or where it comes from.
So in cases where there's only ever one kind of key for a given model (which is almost every model), I like to create a class method for generating that key, which adds a bit of semantic clarity:
class Book(ndb.Model):
title = ndb.StringProperty()
pages = ndb.IntegerProperty()
#classmethod
def make_key(cls, isbn):
return ndb.Key(cls, isbn)
b = Book.make_key('1234-5678').get()
Sure the added code is not strictly necessary, but it adds clarity and makes my models more long-term maintainable.
You can parse the id to key string:
key = ndb.Key(YourModel, id).urlsafe().
and then:
result = YourModel.query(YourModel.key== key).get().

JDO: referencing a collection of entities "owned" by another class

I have a RecipeJDO that contains a List<IngredientJDO>. RecipeJDO "owns" the ingredients. This has been working well for me for several weeks. Now I'd like to introduce a new class "GroceryListJDO", that references the ingredients owned by various recipes.
When I try to persist a new GroceryListJDO I get the following:
javax.jdo.JDOException: Duplicate property name: ingredients_id_OWN
NestedThrowables:
org.datanucleus.exceptions.NucleusException: Duplicate property name: ingredients_id_OWN
javax.jdo.JDOException: Duplicate property name: ingredients_id_OWN
Seems like there is an issue of "ownership" of the ingredients between RecipeJDO and GroceryListJDO.
I could probably change GroceryListJDO to merely contain a List<String> that acts as a kind of foreign key to IngredientsJDO, but that kind of defeats the purpose of using ORM- I'd have to manually fetch and attach the ingredients in my DAO.
What is the best way to manage JDO collections that need to "attach" to multiple container JDO classes?
This is with JDO on Google App Engine, FWIW.
Apparently, this is known as an "unowned" relationship, and is not directly supported in GAE. The workaround is what I feared: only one JDO class can own the collection; any other JDOs that reference these objects must persist only Keys, and manage the fetching/saving of the referenced objects manually.

Resources