How to programmatically identify and list fields with unique=True in any given model - django-models

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().

Related

Django make an object of a model a field of another model

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.

Salesforce - Refer custom field from standard object in custom object

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.

Relating one model to multiple models based on field

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.

trying to sort on a reference property of an entity but don't want to sort on object but a ppty of the object

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).

Load database table in controller cakephp

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.

Resources