Entity classes

The data model can be created manually if you wish to use separate property names from the data keys, or define custom methods for your objects.

Custom Entity class

Each Service instance has their separate base classes for Entities, Actions and Functions. Use them to define your model:

class Product(Service.Entity):
    __odata_type__ = 'ProductDataService.Objects.Product'
    __odata_collection__ = 'Products'

    name = StringProperty('ProductName')
    quantity_in_storage = IntegerProperty('QuantityInStorage')

Note that the type (EntityType) and collection (EntitySet) must be defined. These are used in querying and saving data.

Custom base class

Define a base. These properties and methods are shared by all objects in the endpoint.

from odata.entity import declarative_base
from odata.property import IntegerProperty, StringProperty, DatetimeProperty

class MyBase(declaractive_base()):
    id = IntegerProperty('Id', primary_key=True)
    created_date = DatetimeProperty('Created')
    modified_date = DatetimeProperty('Modified')

    def did_somebody_touch_this(self):
        return self.created_date != self.modified_date

Define a model:

class Product(MyBase):
    __odata_type__ = 'ProductDataService.Objects.Product'
    __odata_collection__ = 'Products'

    name = StringProperty('ProductName')
    quantity_in_storage = IntegerProperty('QuantityInStorage')

    def is_product_available(self):
        return self.quantity_in_storage > 0

Use the base to init ODataService:

Service = ODataService(url, base=MyBase)

Unlike reflection, this does not require any network connections. Now you can use the Product class to create new objects or query existing ones:

query = Service.query(Product)
query = query.filter(Product.name.startswith('Kettle'))
for product in query:
    print(product.name, product.is_product_available())