python-odata
python-odata copied to clipboard
Add support for deferred NavigationProperty handling in manual models
Currently NavigationProperty objects need to refer to an Entity class. Two classes referencing eachother does not work unless NavigationProperties are added after everything else. Should we support Entity refs as strings? How to get the Entity behind the string?
It would be really helpful to be able to pass the Entity refs as strings.
In the meantime, a workaround might be to specify the NavigationProperties in the __new__ method. This makes for somewhat ugly code, but it solves the problem when entities or types aren't declared in advance (possibly due to referencing each other), but will be available when the class is instantiated.
For example:
class ProductPart(Service.Entity):
__odata_type__ = 'ODataTest.Objects.ProductPart'
__odata_collection__ = 'ProductParts'
id = IntegerProperty('PartID', primary_key=True)
name = StringProperty('PartName')
size = DecimalProperty('Size')
product_id = IntegerProperty('ProductID')
def __new__(cls, *args, **kwargs):
cls.product = NavigationProperty('Product', ProductWithNavigation,
foreign_key=ProductPart.product_id)
return super(ProductPart, cls).__new__(cls, *args, **kwargs)
# in Python 3 code, super(ProductPart, cls) can be simplified to super()```