tuomur / python-odata

A simple library for read/write access to OData services
MIT License
79 stars 59 forks source link

Add support for deferred NavigationProperty handling in manual models #3

Open tuomur opened 9 years ago

tuomur commented 9 years ago

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?

tpow commented 5 years ago

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