I was wondering if there was a way to make all objects of a model be fields in another model. For example say I have objects that are the names Bob, Steve, and Matt. I would want those names to be a column in another table.
Related
I have a custom object. I would like to refer the custom field from opportunity object in my custom object as a read only field. How to do that?
You'd need some relation between the 2 tables in database. For example if you add a lookup field in your custom object, call it Opportunity__c & populate it on few recods, then you can display Opportunity Name but also reference other Opportunity fields in reports or formula fields. You could make another field (type = formula and then pick what matches the field you're interested in, text, date, checkbox...). Make the formula similar to Opportunity__r.MyCustomFieldOnOpportunity__c and you're good to go.
If you can't define any sensible relation between Opp and your custom object you'd have to tell us what exactly you try to achieve. Perhaps there's config (or code) way to copy values across. But making a lookup field and then a formula that jumps through this lookup is the most natural way.
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().
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.
I'm trying to join two tables that are in two different databases. These databases don't necessarily have to be the same. So i'm trying to see if I can make a model off of one model and another model off the other model and join the two derived models. Or if there is a way that you know how to join two models of different databases that'd be great too.
It is not possible for a Model to use two Tables/Databases
A model is generally an access point to the database, and more specifically, to a certain table in the database. By default, each model uses the table who's name is plural of its own, i.e. a 'User' model uses the 'users' table.
Source
The second Answer says it is possible, but I don't think it will work the way you want it.
However it is possible for a Model to use a different Database.
You can then create a relationship to link them.
It could be achieved with useDbConfig easily (just tested).
Another way is to use DB view.
I made a model for my pre-set database table.
Now in my controller's action, I have:
$this->loadModel('Preset');
$preset = $this->Preset->find();
Each pre-set row in the table has multiple fields, I want the $preset variable to have all the information from one row. How would I search for the row by name and then grab all other fields from it?
Assuming your model is valid:
$preset = $this->Preset->findAllByName('foo');
What you're trying to accomplish is really quite simple and well-covered in CakePHP's documentation.