Virtual Fields

From Dabo Wiki
Jump to: navigation, search

This wiki article is a explanation of Virtual Fields for bizobjs. Virtual Fields are seen and accessed just like regular data fields. The only exception is they are not stored in the database. These fields are compiled at runtime. A good example of a virtual field is a subtotal for an order item.

As an example, imagine the we have a table that keeps track of items added to an order. The table has several fields:

auto-incrementing ID fields
ID for order
ID for item

This is all fine and dandy, except for we want an easy way to calculate the sub-total. We don't store this information in the database because it's redundant and we can get it from the quantity and price. So, we use a virtual fields to calculate the subtotal for a row dynamically. Take a look at the following code for the Bizobj:

class OrderItemsBizobj(
    def afterInit(self):
        self.DataSource = "orderItems"
        self.KeyField = "ID"
       self.VirtualFields = {"SubTotal": self.getSubTotal}
    def getSubTotal(self):
        return self.Record.Price * self.Record.Quantity

Now, this seems pretty straight forward, right? The VirtualFields property is just a dictionary of key/value pairs where the key is the name of the field and the value is the function to call to get the value. You can now use the field SubTotal as if it were a regular bizobj field. Every time we need to get the value of SubTotal for a row, the getSubTotal() function is called.