I am trying to understand the code used in my new job's web-application. In their models.py file, I see that they call a lot of classes with the .query method, which will then be set equal to a new variable. For example:
def child_variant(self):
#for checking if the current variant is an original variant and has a child variant (the hidden variant record) and if so returning it
child_variant = Variant.query.filter(Variant.original_variant_id==self.id).first()
if child_variant:
return child_variant
else:
return False
I am not sure if the .query method has a special purpose, for example running through their database, and is referring to a sort of SQL command, or if it is referring to some sort of jQuery command.
Related
Python's peewee orm library allows me to query database using the get method like this:
grandma = Person.get(Person.name == 'Grandma L.')
This query will filter the resultset based on Person's name doing a sql where behind the scenes.
I know the expression Person.name == 'Grandma L.' is evaluated first and the get method receives just a boolean value.
How does the get method inspect it's arguments to detect that the filter needs to be applyed to the 'name' field ?
PS: I read peewee's source but couldn't have any clue how it does that.
I know the expression Person.name == 'Grandma L.' is evaluated first and the get method receives just a boolean value.
I don't know Peewee specifically but I do know how this sort of thing is done.
Basically, the second part of your sentence isn't true. name is of a custom type. Its __eq__ method doesn't return just a Boolean value, but rather an object that contains information on what comparison was actually done. (This class might even derived from bool so it works like a Boolean in other contexts.) Its other rich comparison methods are similar.
But how does Person.name know that its name is name? The answer to that is that Person probably doesn't actually have a name attribute. Instead, it has a __getattr__() method that returns the name object, which is the custom class that has the __eq__ method I just described.
Since Person.__getattr__() receives the name of the attribute, it can bake that name into the value it returns for name. Then the custom __eq__ method on name returns a Boolean-like object that contains some representation of name == 'Grandma L.' inside it. And Person.get() uses this representation to put together the query.
i would like to know how onchange function works with boolean and integer fields.
Suppose if one boolean field get changed to True, the value of respective integer should be changed.
Thanks in advance.
#api.onchange
This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form:
#api.onchange('fieldx')
def do_stuff(self):
if self.fieldx == x:
self.fieldy = 'toto'
In previous sample self corresponds to the record currently edited on the form. When in on_change context all work is done in the cache. So you can alter RecordSet inside your function without being worried about altering database. That’s the main difference with #api.depends
At function return, differences between the cache and the RecordSet will be returned to the form.
View management
One of the great improvement of the new API is that the onchange are automatically inserted into the form for you in a simple way. You do not have to worry about modifying views anymore.
Warning and Domain
To change domain or send a warning just return the usual dictionary. Be careful not to use #api.one in that case as it will mangle the dictionary (put it in a list, which is not supported by the web client).
Reading code modules of Tryton, I met a lot this method but I did not figure out what this is for.
What's the use of this function in Tryton?
#classmetod
def __register__(cls,module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor
table = TableHandler(cursor,cls,module_name)
super(Adress,cls).__register__(module_name)
table.not_null_action('sequence', action='remove')
The __register__ method is called every time the model is updated, and it's used to alter the database structure of the current module. Normally tryton, creates all the missing fields for you (this is done on ModelSQL class), but some actions are not possible to be detected automatically, so you must write a migration for it. This is done on the __register__ method of the model.
The code you copied ensures that the sequence field is nullable and if not, it alters the column from null to not null.
I have probably not grasped the use of #hybrid_property fully. But what I try to do is to make it easy to access a calculated value based on a column in another table and thus a join is required.
So what I have is something like this (which works but is awkward and feels wrong):
class Item():
:
#hybrid_property
def days_ago(self):
# Can I even write a python version of this ?
pass
#days_ago.expression
def days_ago(cls):
return func.datediff(func.NOW(), func.MAX(Event.date_started))
This requires me to add the join on the Action table by the caller when I need to use the days_ago property. Is the hybrid_property even the correct approach to simplifying my queries where I need to get hold of the days_ago value ?
One way or another you need to load or access Action rows either via join or via lazy load (note here it's not clear what Event vs. Action is, I'm assuming you have just Item.actions -> Action).
The non-"expression" version of days_ago intends to function against Action objects that are relevant only to the current instance. Normally within a hybrid, this means just iterating through Item.actions and performing the operation in Python against loaded Action objects. Though in this case you're looking for a simple aggregate you could instead opt to run a query, but again it would be local to self so this is like object_session(self).query(func.datediff(...)).select_from(Action).with_parent(self).scalar().
The expression version of the hybrid when formed against another table typically requires that the query in which it is used already have the correct FROM clauses set up, so it would look like session.query(Item).join(Item.actions).filter(Item.days_ago == xyz). This is explained at Join-Dependent Relationship Hybrid.
your expression here might be better produced as a column_property, if you can afford using a correlated subquery. See that at http://docs.sqlalchemy.org/en/latest/orm/mapping_columns.html#using-column-property-for-column-level-options.
I'm using the couchdb.mapping in one of my projects. I have a class called SupportCase derived from Document that contains all the fields I want.
My database (called admin) contains multiple document types. I have a type field in all the documents which I use to distinguish between them. I have many documents of type "case" which I want to get at using a view. I have design document called support with a view inside it called cases. If I request the results of this view using db.view("support/cases), I get back a list of Rows which have what I want.
However, I want to somehow have this wrapped by the SupportCase class so that I can call a single function and get back a list of all the SupportCases in the system. I created a ViewField property
#ViewField.define('cases')
def all(self, doc):
if doc.get("type","") == "case":
yield doc["_id"], doc
Now, if I call SupportCase.all(db), I get back all the cases.
What I don't understand is whether this view is precomputed and stored in the database or done on demand similar to db.query. If it's the latter, it's going to be slow and I want to use a precomputed view. How do I do that?
I think what you need is:
#classmethod
def all(cls):
result = cls.view(db, "support/all", include_docs=True)
return result.rows
Document class has a classmethod view which wraps the rows by class on which it is called. So the following returns you a ViewResult with rows of type SupportCase and taking .rows of that gives a list of support cases.
SupportCase.view(db, viewname, include_docs=True)
And I don't think you need to get into the ViewField magic. But let me explain how it works. Consider the following example from the CouchDB-python documentation.
class Person(Document):
#ViewField.define('people')
def by_name(doc):
yield doc['name'], doc
I think this is equivalent to:
class Person(Document):
#classmethod
def by_name(cls, db, **kw):
return cls.view(db, **kw)
With the original function attached to People.by_name.map_fun.
The map function is in some ways analogous to an index in a relational database. It is not done again every time, and when new documents are added the way it is updated does not require everything to be redone (it's a kind of tree structure).
This has a pretty good summary
ViewField uses a pre-defined view so, once built, will be fast. It definitely doesn't use a temporary view.