neo4j-contrib / neomodel

An Object Graph Mapper (OGM) for the Neo4j graph database.
https://neomodel.readthedocs.io
MIT License
961 stars 232 forks source link

Property instances of unsaved node objects behave unexpectedly #72

Closed pkatseas closed 10 years ago

pkatseas commented 11 years ago

Let's assume a StructuredNode subclass Foo. When instantiating a Foo object (or in fact even through the Foo class itself), any Property instances of the object which don't have a default value specified in their definition will return something equivalent to <neomodel.properties.StringProperty at 0x2de36d0> when referring to them.

This is not very intuitive and can actually cause problems when trying to check against such values by doing for example if myobj.name:, as that will always be True (since there's a Property object in place). One would then have to be aware of the PropertyType and do some custom checking along the lines of if isinstance(myobj.name, basestring):, which is not ideal.

I suppose there are a number of ways to deal with this situation. Django for example hides fields of a model instance behind a Meta class. So if you try to access a field from the model class (without an instance of the class), you get an AttributeError. If you do instantiate and try to access the field, you get a default value for it (empty string, False, None, etc.). Hiding away the properties doesn't sound like the best way forward, but default values do. For example, if there is a default value specified for a property, use that, otherwise return None.

I hope we can start a discussion on this and figure out what works best before moving on to implementing a fix. Also, as this might break existing code, it would probably be better to include the fix in the next major version.

robinedwards commented 10 years ago

Fixed on neo4j 2 branch