Here's a brief description of the models:
Model A represents a piece of equipment and has a name and a state
Model B represents a specific state of many model A's
Model B should relate to many model A's but only for a specific state of model A
Is it possible to model something like this in the datastore? I need the state of model A to be independent of any model B's, but when I peer into a model B I need to know what the state of the model A's should be that the model B is representing.
The current way I am achieving this is by making model B have string fields representing the different model A's with the name of the field being the name of the model A and the value of the field being the state the model A is supposed to be in.
This works, however it's completely static and requires manually adding fields into model B when the number of model A's change. I'm looking for a dynamic approach to solve this problem.
I hope this isn't too confusing, please ask for more clarification if it's needed.
You can use reference properties for this:
class A(db.Model):
state = db.ReferenceProperty(collection_name="equipment")
When you create A, you set the state property to the corresponding B entity.
This also creates a property in the B entity called equipment that can be used to get all the A entities that reference the particular B.
Suppose you have a B entity for broken equipment in a variable broken. You can get all the broken equipment this way
broken.equipment.get()
This is also available with ndb but the details are a little different and you'll need to check the docs for that.
Related
My friends,
How would I do to identify, in any given model, which fields have the parameter unique=True? And how about unique_together. Is there a way to programmatically identify fields with this characteristics?
Any instance from any model have a method called _get_unique_checks() that will return a list of tuples with all the fields that are marked as unique.
Similarly, any instance of any model have another method called _check_unique_together().
I have to model the following use case:
I have record A and B related by a master-detail relationship
I need to hold on A informations from the most recent B (let's say B it's some sort of query to an expensive data source)
I need to hold on A the navigable reference to the most recent B (i.e. the one I selected as data source for A)
How can I model this? Is the only possibility to go through code / a custom VisualForce controller to make this link navigable ?
Say I have done a query and I want to then order it by a reference property attribute - can I temporarily add a field in the results of my query and add in the attribute to sort it before I send it to the template?
q = LetterTable.all().order('votes)
but then I want to take the q results above and sort on a property of LetterTable called "Person". However, 'person' is a reference to an instance on the PersonTable, so I don't want the reference object, I want to sort by person.name. How can I do this?
Anyone?
You can't, at least not stright out of the box. There's some page in the GAE docs that describe how queries work. Essentially your entities are indexed by property, and when you run a query, it just looks through the index.
In your case, the index contains the reference property and not the PersonTable entity it refers to, so the index doesn't have person.name.
The appropriate way to do this in App Engine is to denormalize. That means storing an extra copy of some data you need, in this case, you'd store a copy of person.name inside your LetterTable entity. Then you can query and sort on that name.
class LetterTable(db.Model):
person = db.ReferenceProperty(Person)
person_name = db.StringProperty()
This method certainly has its drawbacks, the primary being keeping data in sync (ie, if a person changes their name, you'll have to find all LetterTable instances that refer to that person, and change all the denormalized names).
Is it a good or a bad practice to have a Backbone model where its id is only unique among a collection but not unique globally? (because contextualized to that collection)
For instance I have a model A which contains bs a collection of model B.
The B collection belongs to the model A.
If I have a1 and a2 two instances of A, Can I use a B with id=1 in a1 and another B with id=1 in a2 ?
Maybe I don't see all the implication but it quite make sense, I just wanted to check with you before using it massively!
Thanks
If the 2 are separate collection you can definitely have id's that are unique to that collection.
Unless the 2 collections interact with each other which may cause any collision in ids
The question is that they represent the same entity or not...if yes their id can be the same otherwise I wouldnt recommend that. If they are the same entities they should be the same reference to a single object.
You will always have an autogenerated unique identifier for all Backbone models, the cid attribute.
I've got three tables (there's actually several more, but I only need the three for this problem). Applications, Appattrs and Appcats. In CakePHP parlance (as best as I can since I'm still learning the framenwork) Applications hasMany Appattrs and Appattrs belongsTo Applications. Easy.
The problem comes when I want to associate Appattrs and Appcat - the association is predicated on a field value and a corresponding foreign key in Appattrs. For instance:
If appattrs.type = 'appcatid' then appattrs.value would point to a record in the Appcat table.
The appattrs table holds static data appattrs.type='dateadded' and value='201201011300' as well as foreign key references. I'd rather not get into a discussion as to why data is stored this way, I just want to figure out how to create associations that will let me pull an application record, the associated attr records and then an attr record with its associated row from the appropriate table. Dynamically.
It seems to me that I should be able to create a model based on a query and then associate that model - I just can't seem to figure out how to do that.
--
If I need to post schema for the three tables, I can. I can also post my current model code, but honestly, right now it's just association variables so I don't think it'll get anyone anywhere.
Thow I do not understand the logic behind this design, I thing what you are looking for
is Creating and Destroying associations on the fly.
On this section of CakePHP Docs, it describes how you can associate models from within the corresponding controller.
So, for example, when you want to save specific data to Appattr model you can do some data checking and create your association using bind() method.
A very abstract approach to the above would be something like this
public function yourmethod() {
...
if ($this->request->data['Appattr']['type'] == 'sometype') {
$this->Appattr->bindModel(
array(/*Your association*/ => array(/* Your attributes...*/)
);
/* Rest of the logic follows */
}
}
This way you get your job done, but it's very possible to end up having very complicated
data in your database and thus having very complicated code.
I hope this helps